Why a NonUniqueObjectException is thrown ?
A NonUniqueObjetException is thrown because hibernate is not able to decide what version of an entity it has to persist.
In this case, it will throw something like this :
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.davidgimelle.hibernate.examples.Role#1]
at org.hibernate.engine.StatefulPersistenceContext.checkUniqueness(StatefulPersistenceContext.java:590)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:284)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:223)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:89)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:507)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:499)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:495)
at com.davidgimelle.hibernate.examples.TestLostSession.commitADetachedObjetThrowAnNonUniqueObjectException(TestLostSession.java:53)
This exception has been thrown because hibernate has tried to persist 2 instances of the same entity, with the same id but not the same content.
For example : role1(id:1, name:"admin") and role2(id:1,name"user")
Usually that happens when an entity is persisted and detached from the session. Another instance of this entity is requested to hibernate. This second instance stayed attached to the session. The first instance is modified.
When hibernate persit the second instance, it is not able to choose what is the correct content to persist and throw an NonUniqueObjectException.
It easier to understand with an example. This piece of code will throw and display an NonUniqueObjectException :
Role role = new Role().withName("Customer");
//Persist a first Role Entity and close the session
Session openSession = sessionFactory.openSession();
Transaction beginTransaction = openSession.beginTransaction();
openSession.persist(role);
beginTransaction.commit();
openSession.close();
//Now role is detached from the session
//change a value in this role
role.setName("new role Name");
//Open a new session
openSession =sessionFactory.openSession();
//Instanciation of the same role from the new session
Role roleFromDB= (Role) openSession.get(Role.class, role.getId());
//Now role and roleFromDB have the same id, but role is detached
Transaction beginTransaction2 = openSession.beginTransaction();
//The next line will throw an exception
try{
//Save role and throw an exception
openSession.saveOrUpdate(role);
//This line will be never executed
beginTransaction2.commit();
Assert.fail();// Fail this test if this line is reached
}
catch(NonUniqueObjectException nuoe){
//Display this exception
nuoe.printStackTrace();
}
openSession.close();
You can find a complet maven project able to run this exemple in a Junit test at http://www.davidgimelle.com/src/hibernate_examples_dg-src.1.1.1-SNAPSHOT.zip
References
- SaveOrUpdate versus Merge : http://www.stevideter.com/2008/12/07/saveorupdate-versus-merge-in-hibernate
- Saving detached entities : http://blog.xebia.com/2009/03/jpa-implementation-patterns-saving-detached-entities

Last comments