2

Possible Duplicate:
Hibernate: different object with the same identifier value was already associated with the session

I am working with Hibernate and Struts 1. My web application is running on one machine and my Hibernate code on another. I am using web services to access the database. My problem is that when I attempt to update any object it throws "org.hibernate.NonUniqueObjectException", when I call commit via my service method.

Caused by: org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [..emp#2]

I am unable to find the what the problem is, but I think when I retrieve the object for editing, it gets attached to the Hibernate session and then when I attempt an update it creates a new object on the service side as I am deserializing the object, so there may be two object with same identifier.

Does anyone have any insight into what might be going on with this?

Community
  • 1
  • 1
pdubey
  • 33
  • 3

1 Answers1

2

You need to merge your detached objects back into the Hibernate Session prior to committing them. Using the (legacy) HibernateUtil class, your code would look something like:

final Session session = HibernateUtil.getInstance().getSessionFactory().getCurrentSession();
// ...
session.merge(entity);

Also see here.

Community
  • 1
  • 1
Perception
  • 79,279
  • 19
  • 185
  • 195