• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getting NonUniqueObjectException exception

 
sujith Acharya
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having 2 tables A and B, where B is the child of A, also there is relation ship between B and B

Scenario:
a is the record in A
b1,b2 are the records in B

b1 and b2 are children of a,
also b2 is children of b1.
Which leads to b1 having parent a, b2 having parent a,b1

I have coded such that,
if A is updated -> its child B has to be updated ->child B of B has to be updated



when I try to update a, it updates b1 first, and since b2 is child of b1, b2 will get updated.

in next iteration, its b2's turn to get updated since b2 is the child a, at this point of time I am getting "org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session" this error, since b2 already got loaded into session when b1 is updated, but when a tries to update b2, i m getting this error.

Please help me how can I solve this issue... please let me know if you need more information.

Thanks in advance
 
Angel Taveras
Ranch Hand
Posts: 84
Eclipse IDE Hibernate Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi sujith, did you try to use merge instead of saveOrUpdate?
 
sujith Acharya
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Angel,

I tried merge() instead of saveOrUpdate(). The exception doesnt come if I use merge(). but the update made to object(b1)by one parent object(b1) is lost when the object is saved after updating by another parent object(a1).

Is there anyway to retain the changes made by one parent while updating by another parent in a single transaction?
 
Angel Taveras
Ranch Hand
Posts: 84
Eclipse IDE Hibernate Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It depends on the cascade behavior that you use on the relationship. There are four basic cascade behavior: save, update, delete, merge. I don't know if the "all" cascade behavior will be triggered by the merge event, because the merge concept was introduced when JPA arrived, but you could try it and see...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic