Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Non Unique object Exception

 
Ashok Kumar
Greenhorn
Posts: 7
Chrome Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
javax.persistence.PersistenceException: org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session:

I have mapped the files coorectly but i got this exception frequently..
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually that error occurs when you try to "attach" an object that has the same id as an object that is already being managed.

For instance I can do this

MyObject someObjectICreateThatIsNotManaged = new MyObject();
someObjectICreateThatIsNotManaged.setId(1); //Yes setId should not be changable by code, but this is just for demonstration purposed

(Now within a Transaction/Session)
MyObject objectWithID1 = em.find(1,MyObject.class));
em.persist(someObjectICreateThatIsNotManaged); // This will get that exception thrown because objectWithID1 was loaded and is managed by the EntityManager, and when you then tried to "persist" someObjectICreateThatIsNotManaged, that object has the same id of 1 in it, and

someObjectICreateThatIsNotManaged != objectWithID1

Hope that helps clarify it.

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