File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes Non Unique object Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Non Unique object Exception" Watch "Non Unique object Exception" New topic

Non Unique object Exception

Ashok Kumar

Joined: Oct 03, 2007
Posts: 7

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

Joined: Feb 05, 2001
Posts: 17276

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.


Perfect World Programming, LLC - iOS Apps
How to Ask Questions the Smart Way FAQ
I agree. Here's the link:
subject: Non Unique object Exception
It's not a secret anymore!