[jboss-user] [EJB 3.0] - Re: onetomany relationship question

Wolfgang Knauf do-not-reply at jboss.com
Wed Jul 8 16:34:24 EDT 2009


Hi,

I played around with your sample a bit. I agree that it works if you first create and save the parent, then create the child, set the parent and persist the child.
But assume that you don't save the parent, but the child. Here is the code snippet: 
	ParentBean parent = new ParentBean();
  | 	entityManager.persist(parent);
  | 
  | 
  | 	ChildBean child1 = new ChildBean();
  | 	parent.getChilds().add(child1);
  | 	//child1.setParent(parentNew);
  |     
  | 	entityManager.persist(parent)
  | 
In this case, child and parent will be saved without error messages, but the relationship related database data is missing.

So, as a general guideline: always update both sides of the relationship, this will avoid a lot of problems ;-).

Best regards

Wolfgang

View the original post : http://www.jboss.org/index.html?module=bb&op=viewtopic&p=4242479#4242479

Reply to the post : http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&p=4242479



More information about the jboss-user mailing list