Hibernate Error: org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session
What is the reason of such exception?
Session already contains object that you are trying to put there one more time with such identifier (for example with update). It also could be many-to-one or one-to-many relationship object or cascading operation.
How to deal with it?
What is the reason of such exception?
Session already contains object that you are trying to put there one more time with such identifier (for example with update). It also could be many-to-one or one-to-many relationship object or cascading operation.
How to deal with it?
- probably you already called update() for this object, and trying do this 2nd time
- another possible reason you making copy of the object that is already associated with session (has assigned id and was loaded recently) and trying to update it
- try session.merge() - it will copy the state of the given object into the persistent object with the same identifier.
- try session.evict(object) - remove this instance from the session cache.
Nice explanation for those who already knows what to do. It's lacking information. To do what is needed is necessary to search in another place.
ReplyDeletelike where we have to search ?
DeleteThank you for information even it being objective. It helped me a lot. In my case, I needed put persist() and flush() more than one time.
ReplyDelete