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

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

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
[plain]
getHibernateTemplate().saveOrUpdate(myBean);
myBean = null
[/plain]
to
[plain]
getHibernateTemplate().saveOrUpdate(myBean);
//myBean = null
[/plain]

If the above ways did not work for you then try this too,
[plain]
getHibernateTemplate().merge(myBean);
[/plain]

Please post your comments in comments area…..

950 total views, 3 views today

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.