Akshay Salvi
Akshay Salvi

Reputation: 71

An entity with a oneToMany relationship with one table and one ManyToMany relationshp with another table returns duplicate records when rtrieved by ID

I have an Entity class as below, which has 1 oneToMany mapping and 1 manyToMany mapping:

    class Parent {
       
       @Id
       String id;
       
       @Column
       String name;

       @OneToMany
       List<Address> addresses;

       @ManyToMany
       Set<Child> children;
}

I'm aware about creating Foreign key references in the 'oneToMany' relationship, and using a Join table for the 'manyToMany' relationship.

Let's say I have the below data:


Parent table:
_______________
P_id      Name
p1        John


Address table:
_________________________________
A_id      Address      Fk_p_id
a1        London        p1
a2       Amsterdam.     p2


Child table
C_id        Name
c1         child1
c2         child2


Person_Child_Mapping table
p_id   c_id
p1       c1
p2      c2
p3      c3

Now, when I do a get Parent by ID, hibernate returns back a Parent object with 2 'Addresses', but has 6 'Child' objects, where each Child object is duplicated. Is there a reason why that might be happening?

Upvotes: 0

Views: 147

Answers (0)

Related Questions