• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

org.hibernate. NonUniqueObjectException

 
Erap Estrada
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am getting these errors whenever I try to add or delete an object:
org.hibernate.NonUniqueObjectException: "a different object with the same identifier value was already associated with the session."

It is inconsistent though.

It is unidirectional and here is the mapping of involved object:


Any ideas why is this occuring?

Thanks!
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Usually this happens with detached objects. You might be taking an object that isn't associated to a Session, and when you are trying to do the add or delete, there is already an object loaded into the Session that has the same ID as the detached object. When using detached objects, try "merge"ing the detached object to the Session first.

Definition of the exception
http://www.hibernate.org/hib_docs/v3/api/org/hibernate/NonUniqueObjectException.html

Another Javaranch post that might also help
http://www.coderanch.com/t/215214/ORM/java/updating-values-Hibernate

Mark
 
Erap Estrada
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mark. I have read and tried those things in another thread but nothing works.

Somewhere, it is saying that it is a Hibernate bug.

On my project, I have a PK of the object to delete. What I did is, get all the related objects, iterate through it, compare their PK, if their equal, get that object and delete it. It's not the best way but at least it's working.

If you have further input, I appreciate it.

Thanks!
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With the detached object, have you tried to call the merge() method to merge the object with the one that is already in the Session?

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic