in Hibernate , the persistence object can be divided into three periods in the process of being manipulated, and these three periods are related to the period of the Session, because the operations in Hibernate are done based on the Session . The specific method of the Session allows the object to transition from one state to another.
There are three life cycles for persistent objects, transient state, persistent state, and detached state. such as:
Instantaneous state
never persisted, not associated with any Session . Objects in this state are created with the new keyword, where the object does not correspond to records in the database.
Transient status Useruser = new User (), User.setname ("Zhang San"); User.setpassword ("123"); User.setcreatetime (Newdate ()); User.setexpiretime (Newdate ());
Persistent state
Persistent, currently associated with the Session .
Persistent status Session.save (user); User.setname ("John Doe"); Tx.commit ();
After the object is saved through the Save () method of the Session Object , the object becomes persisted. that is, there is a Session, and there is a record in the database. However , the data is stored in the database only after a commit . In this example, after the commit () method is called, the user's name is changed from Zhang San to John Doe.
As an example, instantaneous objects are persisted by means of save,saveorupdate , and so on, and the object passes through the Session the get () or load () method directly becomes the persistent state, and the Free State object passes the update, The Saveorupdate becomes a persistent state.
Persistence Features:
A persistent instance is any instance that has a database identity. It has a persistent manager Session Unified Management, persistent instances of the actual transaction operations.
(1) in the cache of a session instance, it can be said that the persisted object is always associated with a session instance.
(2) Persistent objects correspond to related records in the database.
(3) When the session cleans up the cache, the database is updated synchronously, depending on the properties of the persisted object.
Free State
previously persisted, but is not currently associated with any Session associated.
Persistent status Session.save (user); User.setname ("John Doe"); Tx.commit ();//detached status User.setname ("Harry");
In the above example, after leaving the persistent state, it is impossible to change the user's name again, only to return to the persistent state to update the user's name to update the name to the database. Therefore, the Free State can not affect the data changes in the database.
The Free State can only be converted from a persistent state.
free objects and instantaneous objects: the similarity between the free objects and instantaneous objects is that they are not associated with the Session , and the free objects are converted by persistent objects, so there may be corresponding records in the database. Instantaneous objects do not have corresponding records in the database.
In summary, it is found that the objects in transient state and free State are not session management, Regardless of how the object properties are modified, the data in the database is not affected. When the object is persisted, modifications to the object are performed only when the commit ()