• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

TWO - records are getting saved In OneToMany Bidirectional Relation in Update Operation?

 
Surendra Manikpuri
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new to JPA, using JPA 2.0. I am facing on problem with OneToMany Bidirectional Relation. When i am updating these, TWO - records are getting saved. My Entities are:

1) Claimant: having relationship with its own as;

@ManyToOne(fetch=FetchType.EAGER, optional=true, cascade=CascadeType.DETACH)
private Claimant parentClaimant;
2)With ClaimantAddress:

@OneToMany(mappedBy="claimant", cascade = CascadeType.ALL, fetch=FetchType.EAGER)
private Set<ClaimantAddress> addresses = new HashSet<ClaimantAddress>();
3) and Other Side in ClaimantAddress:

@ManyToOne
@JoinColumn(name="claimant_fk")
private Claimant claimant;
and after updating these eneties I am saving this by using merge().

What is wrong here? I am not able to find out.
 
James Sutherland
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check if the object you are merging has two copies of the new object.

Also check if you are calling merge twice.

Also ensure you are maintaining both sides of the bi-directional relationship correctly.
 
Surendra Manikpuri
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks James for reply;

Yes my merging Object has 2 Copies of the another object.
But That's our requirement... Our Claimant Object has address of two types : Personal and Seasonal and User can update any one OR both the addresses;

So if the above is the scenario then always two records will be saved OR is there any possibility is there that only one object of Claimant will be created and if not then when I am displaying this record I am Setting these values to DTO and taking both the address as a Single line But there i am getting two records with same ID for each of the address.

Is there any way to handle the above case or Need to make any Changes to my Entities ?? Please guide me so that i can resolve this issue

Thanks

Surendra M
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic