Basic concepts of three states:
1. Temporary identity (Transient): Also known as Free State, it exists only in memory, and there is no corresponding data in the database.
Object created with new, long, not in session, the object in this state is called a temporary object;
2, Persistence State (persistent): associated with the session and has corresponding data in the database.
Has persisted. Added to the session cache. objects, such as those saved through hibernate statements. The object in this state is called a persistent object;
3, Free State (Detached): The persistent object is detached from the session object. Objects such as the session cache are emptied.
Feature: persistent, but not in session cache.
The object in this state is called a free object;
x√ |
Temporary status (Transient) |
persisted state (persistent) |
Free status (Detached) |
is in session cache |
x |
√ |
x |
< Is there a corresponding record in the p> database |
x |
√ |
√ |
|
|
|
|
The similarities and differences between free objects and temporary objects:
Both are not associated with the session, and the object properties and database may be inconsistent;
The free object has the persistence object to close the session and transforms, in the memory also has the object so at this time becomes the Free State;
Hibernate and SQL Relationships:
After working with Hibernate methods such as Save () and so on. does not directly generate SQL statements to manipulate the database. Instead, the updates are saved to the session. Only the session cache to be updated, the underlying SQL statement ability to run, data stored in the database.
The following examples illustrate:
One, session.save (user) execution mechanism.
1. Add the user object to the cache. Make it a persisted object.
2. Use the identity generated by the mapping file to generate the ID.
3, run when the session clears the cache: generates an INSERT SQL statement at the bottom, and stores the object in the database;
Note: After you run Session.save (user), before the session cleans up the cache. Let's say you change the User object property value. Then finally the value of the database will be the last change of value, the process of the ID can not be changed;
Second, the session.delete (user) execution process.
Assuming that the user is a persisted object, the delete operation is run and the same underlying database runs with the following conditions: when the session cleans up the cache;
Suppose the user is a free object:
1. Associates the user object with the session. Make it a persistent object;
2. Then, according to the user is the process of persisting object;
Method of conversion between three states:
How does ① become a free State? objects become free states by constructing methods. The persistent and free states become free states by the Delete method of the session.
How does ② become a persistent state? the object can be persisted directly by the load or get method of the session, and the free-state object can pass save. The saveorupdate or persist method becomes a persistent state. The Free State object can become a persistent state through Update,saveorupdate .
How does ③ become a free State? the Free State can only be converted from a persistent state. Implemented by the close or clear method.
Comparison of several conversion methods:
1.get and load
is loaded from the database into the data encapsulated as Java objects, so that the Java object from the Free State directly into a persistent state;
But there are two differences: ①get returns an object that can be null. The load return value is always non-null, throws an exception when it is not found ②get immediately runs INSERT, while load uses this object to run the insert
2.save,update and Saveorupdate
Save is the transition from a free State to a persistent state. The update is a free state into a persistent state , Saveorupdate can be said to be the synthesis of the two, it is run to infer the state of the object (mainly through the absence of a primary key inferred), if Free State, then save. If the Free State. The update
3.save and persist
Both convert an object from a free state to a persistent state, but the return value is different:save returns the primary key value , and persist does not return
4,saveorupdate and Merge
Both the Free State or the Free State object is associated with the database. But the merge does not change the original state of the object
Moreover, to clear and flush the method can also be introduced. Clear this is the session these objects all become free states. Described by a method of the persistence object of a Free State (the second is closed session); Flush in order for the method update operation to be immediate (under normal circumstances). only if the transaction is closed by the update operation ).
Hibernate in the lifetime of a persistent object (three states: a Free State, a permanent state, a transition between free states)