wood burning stoves 2.0*
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
Author

Non Unique object Exception

Ashok Kumar
Greenhorn

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
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Non Unique object Exception
 
Similar Threads
Giving error : ex might not be initialized
Doubt regrding toString()
How to get rid of (check logs) in the stacktrace ?
Arraay doubt
catch (Exception parameter)