[Switch] Three statuses of Hibernate objects and three statuses of hibernate objects
In Hibernate, an object has three states: temporary state (Transient), Persistent state (Persistent), and Detached ).
Objects in the persistent State are also called PO (PersistenceObject), and temporary objects and free objects are also called VO (ValueObject ).
1. temporary status
Java objects in the memory space are opened by the new command, for example:
User user = new User ();
An object exists in memory in an isolated manner. It is a carrier carrying information and has no association with database data.
(A) if there is no variable to reference this object, it will be recycled by gc;
(B) In Hibernate, the instantaneous object can be associated with the database through the save () or saveOrUpdate () method of the session, and the corresponding data can be inserted into the database, in this case, the temporary object is converted into a persistent object.
2. Persistent status
Objects in this state have corresponding records in the database and a persistent identifier. objects obtained through the get () and load () Methods of the session are persistent objects.
After the persistence object is modified and changed, it will not be synchronized to the database immediately until the database transaction is committed. Before synchronization, persistent objects are Dirty (Dirty ).
(A) If the delete () method of hibernate is used, the corresponding Persistent object becomes a temporary object, because the corresponding data in the database has been deleted, this object is no longer associated with database records.
(B) When a session executes close (), clear (), and evict (), the persistent object becomes a free object. Although the object has a database recognition value, however, it is no longer under the management of the HIbernate Persistence Layer.
Persistent objects have the following features:
(1) associate with the session instance;
(2) There are associated records in the database and they have persistent identifiers.
3. Free Status
When the session associated with a persistent object is closed, the persistent object is converted to a free object. when the free object is re-associated to the session, it is converted into a persistent object again (the changes made during the Detached will be persisted to the database ). Free objects have database recognition values, but are no longer within the scope of persistent management.
(A) Through the update (), saveOrUpdate () and other methods, the free object can be converted into a persistent object.
(B) If the delete () method of hibernate is used, the corresponding free object becomes a temporary object. Because the corresponding data in the database has been deleted, the object is no longer associated with the database record.
(C) When no variable references it, it will be reclaimed by gc when appropriate;
Free objects have the following features:
(1) It is essentially the same as an instantaneous object. When no variable references it, JVM recycles it as appropriate;
(2) A database record ID value is more than the instantaneous object.
Example:
Session session = factory. openSession ();
User user = new User ();
// The user object is in the temporary state.
User. setName ("Zhang San ");
User. setAge (18 );
Transaction tran = Session. beginTransaction ();
Session. save (user );
// The user object is converted to a persistent state.
Tran. commit ();
Session. close ();
// The user object is converted to the Free State.
User. setName ("Li Si ");
Session session2 = factory. openSession ();
Transaction tran2 = Session2.beginTransaction ();
Session2.update (user );
// The user object is converted to a persistent state.
Tran2.commit (); (changes to objects in the Free State will be persisted to the database again)
Transaction tran3 = Session. beginTransaction ();
Session2.delete (user );
// The user object is converted to a temporary state.
Tran3.commit ();
Session2.close ();
The impact of different Session operations on Object status:
Session save () method
The save () method converts a temporary object to a persistent object.
Update () method of Session
The update () method converts a free object to a persistent object.
Session lock () method
Call the lock () method to associate the object with the Session without force update.
Session merge () method
Copy the status of a specified object to a persistent object with the same object identifier.
Session saveOrUpdate () method
The saveOrUpdate () method executes the save () method for temporary objects and the update () method for free objects.
Load () and get () Methods of the Session
Both the load () method and get () method can load objects according to the object identifier. The objects loaded by these two methods are located in the Session cache and are persistent objects.
Delete () method of Session
The delete () method is used to delete records corresponding to persistent objects from the database. If a persistent object is input, the Session executes a delete statement. If the input parameter is a free object, associate the detached object with the Session to make it a persistent object, and then execute a delete statement.
Evict () method of Session
The evict () method deletes a persistent object from the Session cache.