Possible Duplicate:
Spring + Hibernate : a different object with the same identifier value was already associated with the session
I’ve loaded an object X from DB using hibernateTemplate find by id, then I get some attributes from that object and added it to another object Y from the same type which was also loaded by the same X id. Then when I tried to saveOrUpdate object Y, hibernate throws exception a different object with the same identifier value was already associated with the session, which I think means that object X is associated with that attribute in the same session, so Y can’t be saved or updated and affect also that attribute.
How can I remove object X from session so it’s no longer associated with that attribute
I tried to use merge instead of saveOrUpdate and it’s working fine, but is it the same as saveOrUpdate? I mean can I depend on it to add new records or update them?
After a lot of tries, I found that using merge is the best approach to handle this effectively, and to take care of new instances to be saved I think best approach is to do this:
So if it was a new instance to session it’ll be done through
saveOrUpdate, and if it’s a duplicated instance for the same rows, it’ll be handled using merge.