. Advertisement .
..3..
. Advertisement .
..4..
I get an error
Hibernate error: "a different object with the same identifier value was already associated with the session: C
when trying to call session.saveOrUpdate(myAObject). How to fix the a different object with the same identifier value was already associated with the session error? Please give me some advice.
The cause:
This error happens due to the B objects do not relate to the same instance of the Java C object. They are both referring to the same database row (i.e. the same primary key), but they are separate copies.
So, the Hibernate session that while manages the entities keeps track of which Java object corresponds to which row with the same primary key.
Solution:
One way is to double-check that the Entities of objects B that refer to the same row are referring to the same object instance of C. Alternatively, you can disable cascade for that particular member variable. As a result, when B persists, C does not. However, you will have to save C separately. If C is a type/category table, it makes logical to keep it that way.
Set cascade to MERGE. That should do it.