[solved] org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session

(Last Updated On: December 20, 2013)

org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session:

Meaning of this exception:If we are trying to save the same identifier (primary key) with different session we will get this error.

How I solved ?I have used Transaction and session previosly, then I replaced those things with getHibernateTemplate,
getHibernateTemplate().saveOrUpdate(myBean);

Then issue got resolved.

In another scenario,getHibernateTemplate().saveOrUpdate(myBean); for the same I got the same exception that is because I assigned null to myBean after save or update so if same identifier comes it producing the issue.

So I changed

getHibernateTemplate().saveOrUpdate(myBean); 
myBean = null 

to

getHibernateTemplate().saveOrUpdate(myBean); 
//myBean = null 

If the above ways did not work for you then try this too,

getHibernateTemplate().merge(myBean);

Please post your comments in comments area…..

242 total views, 1 views today

Leave a Reply

Your email address will not be published. Required fields are marked *