1 Reply Latest reply on Jul 10, 2005 8:22 PM by doofus

    Duplicate Entity Beans One-to-many relationship

    doofus

      Something odd is happening with my entity beans when doing a one-to-many relationship.

      In SQL if i had a one-to-many relationship between car and person and everyone had one car i would expect the following (simplistically):

       p.id | p.name | c.id | c.colour
       -------------------------------------------------
       1 | person1 | 1 | red
       2 | person2 | 2 | green
       3 | person3 | 3 | blue
      
      


      Now, if one person bought a second car i would expect:

       p.id | p.name | c.id | c.colour
       -------------------------------------------------
       1 | person1 | 1 | red
       1 | person1 | 4 | yellow
       2 | person2 | 2 | green
       3 | person3 | 3 | blue
      
      


      If we translate this to EJB-speak i would have a Person entity and a Car entity.

      In the second set of result person 1 is duplicated because of the join criteria. the same thing happens for me in the hibernate query (behind my ejb's) but the problem is that I now have 2 Person entities when there is > 1 car attached to it. This ONLY happens when i am doing an EAGER fetch. When I do a LAZY fetch everything works fine. I guess this is because the join spans less tables.

      My actual program has more tables (4 tables) in the join than this but hopefully this provides an easy to understand description of the problem.

      The actual program is akin to:

      
       (parent 1) 1------* (join table) *------1 (parent 2) 1------* (child 2)
      
      


      I have tried overriding the equals() and hashcode() methods and that didnt make any difference.

      Thanks,

      -luke