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

lpiccoli do-not-reply at jboss.com
Tue Jul 7 02:23:37 EDT 2009


i read the 'EJB3 relationships' wiki article
http://www.jboss.org/community/wiki/EJB3relationships and have a question.

  | @Entity()
  | public class ParentBean implements Serializable
  | {
  |   private Set<ChildBean> listChilds = new HashSet<ChildBean>();
  |   
  |   @OneToMany(mappedBy="parent")
  |   public Set<ChildBean> getChilds()
  |   {
  |     return this.listChilds;
  |   }
  | }
  | 
  | @Entity()
  | public class ChildBean implements Serializable
  | {
  |   @ManyToOne ()
  |   public ParentBean getParent()
  |   {
  |     return this.parent;
  |   }
  | }
  | 
  | 

Q1. essentially why doesnt the following work


  |      ParentBean parent = ...;    
  |      ChildBean child1 = ...;
  |      child1.setParent(parent);
  |      
  |      ChildBean child2 = ...;
  |      child2.setParent(parent);
  | 
  | .....
  |  
  |        ParentBean parentBean = this.entityManager.find(ParentBean.class, int_ParentId);
  |     
  |     
  |       assertEquals( 2, parentBean.getChilds().size() ); <--- why do i get 0
  | 
  | 

Q2. Does the parent.setChilds( list) must be called before calling
parent.getChilds() or can is ok set the relationship with a)child1.setParent(p),
child2.setParent(p)  or b) having the data loaded from scripts?

currently my parent.getChilds() returns null although there is
parent/child relationship on the many-to-one table.

Q3. I was expceting that the parent.getChilds() executes a query like ('from child c where c.parent=:parent' )

is my understanding of one-to-many relationship wrong?



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

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



More information about the jboss-user mailing list