Hibernate


X Hibernate Envers 3.6.10 Final Docs

Hibernate Error: org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session:



You may have recieved an exception in Hibernate like:

org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session…..

This is likely caused by your code doing something like:

session.get()

object.setSomething(value)

session.update()

The session.update() call is attempting to attach an object to the Hibernate session. This is done by the object id. If you already have an object with the same id associated with the session (i.e. maintained by Hibernate) you get this error.

You have two choices:

1) Don’t call update on the object. It is already being maintained by Hibernate. There is no need to do so. A later flush() will push any changes recorded in the session to the database.

2) If you can not tell if the object is associated with the session (likely in a long user scenario) call merge() instead. Merge() takes the instance passed and merges it with any object of the same id associated with the session. Note, if you plan on using the object again you must use the object returned by the call to merge(). The instance object passed to merge will not be updated.

No comments:

Post a Comment