Vendredi 20 mai 2011 5 20 /05 /Mai /2011 13:27

hibernate-icon.jpeg

 

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

Par David Gimelle - Publié dans : Java
Add a comments - View 0 comments
Retour à l'accueil

Summary

  • : GetJ2ee -Java development
  • : Articles about Java and EE
  • Partager ce blog

Contact

You can contact me here

or by Twitter

Search

Calendar

Mai 2012
L M M J V S D
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      
<< < > >>

Profil

  • David Gimelle
  • Java Developer for more 10 years. I worked in France, Canada and Switzerland. I am contractor in London since 2010.

Syndication

  • Flux RSS des articles
 
Créer un blog gratuit sur over-blog.com - Contact - C.G.U. - Rémunération en droits d'auteur - Signaler un abus - Articles les plus commentés