Today, hibernate development encountered such a mistake:
A different object with the same identifier value is already associated with the session
(Different objects have the same identifier value already associated with the session)
Involves the difference between several confusing methods in hibernate:
The first step is to introduce the three states in Hibernate.
There are 3 types of Hibernate objects: Transient (Transient), persistent (persistent), and off-state (Detached). Objects that are in persistent state are also known as PO (Persistence object), and instantaneous and de-tube objects are also known as VO (Value object).
Transient state a Java object that opens up memory space by the new command, eg. person person = new person ("xxx", "xx");
The object is not referenced by a variable, it is recycled by the Java Virtual machine. Instantaneous objects exist in memory, it is the carrier that carries the information, does not have any relation with the data of the database, in
Hibernate, you can use the Save () or Saveorupdate () method of the session to associate the instantaneous object with the database and insert the data corresponding to the inserted database
, the instantaneous object is transformed into a persisted object.
An object in this state with persistent states has a corresponding record in the database and has a persistent identity. If it is
With Hibernate's Delete () method, the corresponding persistent object becomes instantaneous, because the corresponding data in the database has been deleted, and the object is no longer associated with the database record. When a
After the session executes close () or clear (), evict (), the persisted object becomes a de-tube object, at which time the persisted object becomes a de-tube object, although the object has data
The library recognizes the value, but it is not under the management of Hibernate persistence layer.
Persistent objects have the following characteristics:
1., and the session instance Association;
2., there is a record associated with it in the database.
Off-Pipe State
off-state when the session associated with a persistent object is closed, the persisted object is transformed into a de-tube object. When the off-tube object is re-associated to the session, it is again transformed into a persistent object. The de-tube object has the recognition value of the database and can be transformed into a persistent object by means of update (), Saveorupdate () and so on.
The de-tube object has the recognition value of the database and can be transformed into a persistent object by means of update (), Saveorupdate () and so on.
The off-pipe object has the following characteristics:
1. Essentially the same as an instantaneous object, when no variable references it, the JVM will recycle it at the appropriate time;
2. More than the instantaneous object a database record identification value.
Come down to my understanding of the difference between merge and Saveorupdate methods: The merge method is to transform the object we provide into a managed state object, whereas saveorupdate turns the object we provide into a persisted object. , said the popular point is: Saveorupdate after the object will be included in the session management, the state of the object will be synchronized with the database, query the object will be directly from the session, the merge object will not be included in the session management, The object will be queried again or taken from the database.
So encountered "a different object with the same identifier value is already associated with the session" This issue only needs to empty the session or save The Orupdate method changes to the merge method.
The difference between Memge and saveorupdate