Hibernate 's PO objects have three states: temporary (aka transient), persistent (also known as persistent), and off-state (also known as off-tube, free states). Objects that handle persistent states are also known as PO, temporary objects, and off-pipe objects, also known as VO.
1. Temporary state:
Simply put, the Java object that was just generated when the new command opens up the memory space is in a temporary state.
Like what:
User user = new user ();
If the object is not referenced by a variable, it will be reclaimed by the Java virtual machine.
The temporary object is isolated in memory, it is the carrier that carries the information, and does not have any correlation with the data of the database. In hibernate , you can use the Session 's Save () or savaorupdate () method to associate a temporary object with a database and insert a database into a persisted object.
2. Persistent State:
Persistent state, which has a corresponding record in the database and has a persistent identity (for example, anInteger ID corresponds to a primary key of the record table). If the Dao Layer method in hibernate Deletes (), the corresponding persisted object is changed to a temporary object. The records in the corresponding database are also deleted and are no longer associated with the records of the database.
To summarize, the characteristics of persistent objects:
(1) association with Session instance;
(2) a record associated with it in the database;
3. Off-tube state (Free State):
When the Session associated with a persisted object is closed, the persisted object is turned into a de-tube object. When the off-tube object is re-associated to the session , it is again transformed into a persisted object.
Note: The de-tube object has a recognized value for the database and can be transformed into a persistent object by means of update (),savaorupdate () , and so on.
Characteristics of Off-pipe objects:
(1) is essentially the same as a temporary object, and when no variable references it,theJVM reclaims it at the time of verification;
(2) more than the temporary object a database record identification value;
4, the role of common methods in the Session to PO Object State
Temporary state, persistent state, and de-state (i.e., Free State), three state conversions are done bySessionto invoke, the transient-to-persistent state method hasSave(),saveorupdate(),Get(),Load(); Persistent to transient method hasDelete(), the Free State to the persistent state method hasUpdate(),saveorupdate(),Lock(); The method for persisting to a Free State is:Session.close(),session.evict(),Session.clear().
5 . The difference between save () and update ()
Save () is the function of saving a new object,update() can be a de-state of the object (must be corresponding to a record) updated to the database.
6, update () and saveorupdate () difference
The Saveorupdate () method essentially synthesizes save () and update ()
Typically the following scenarios are used to update () and saveorupdate ():
(1) The program loads the object in the first session and then closes the session () ;
(2) The object is passed to the presentation layer;
(3) Some changes have taken place in the object;
(4) The object is returned to the business logic layer and finally reaches the persistence layer;
(5) The program creates a second sessionand invokes the update () of the second session method to persist these changes
Status of Hibernate PO objects