PO persistence object and PO in hibernate
I. Understand the persistence object PO:
In hibernate applications,Each table in the database corresponds to a persistent object PO.. PO can be viewed as a java object mapped to a database table. The simplest PO corresponds to a record in a table in the database. Multiple records can correspond to a set of PO.
The PO class is a persistence class, which is actually a common JavaBeanOnly declare that a certain rule is a PO.
package PO;public class UserInfoPO { private int id; private String username; private String password;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;} }
PO definition follows the following three principles:
1. Declare private attributes for persistent fields and provide getter and setter Methods
2. Implement a no-argument constructor. All persistence classes must have a default constructor, so that hibernate can use newInstance () to instantiate them.
3. Provides an identifier attribute.
Identifier attribute, used to map the primary key fields of the database table. This attribute can be called by any name, and its type can be any original type, the packaging type of the original type, String or Date.
2. Three States of a persistent object:
The PO object of Hibernate has three statuses: temporary, persistent, and unmanaged.
1. Temporary State
ByThe new command opens up the memory space. The newly generated Java object is in the temporary state.. For example, UserInfoPO ui = new UserInfoPO (); if no variable is referenced, It is recycled by the Java Virtual Machine.
A temporary object exists in the memory. It carries information and does not have any association with the database data. In Hibernate,You can use the save () or saveOrUpdate () method of the Session to associate the temporary object with the database and insert the data into the database. In this case, the temporary object becomes a persistent object.
Temporary object features. Temporary objects have the following features:
(1)Not in Session cacheIs not associated with any Session instance.
(2) In the databaseNo corresponding record.
(3) TheyNo behaviors associated with database tables
In the following cases, the Java object enters the temporary state:
(1)When a Java object is just created using the new statement, it is in the temporary state,This does not match any records in the database.
(2)The delete () method of Session can convert a persistent object or a free object to a temporary object.. For a free object, the delete () method deletes the corresponding records from the database. For a persistent object, the delete () method deletes the corresponding records from the database, and delete it from the Session cache.
2. Persistent state
Objects in this state have corresponding records in the database and a persistence flag. If the delete method is used. The corresponding Persistent object becomes a temporary object. Because the corresponding data in the database has been deleted, this object is not associated with the database records.
After the session executes close () and clear (), the persistent object becomes the unmanaged object.In this case, the persistent object will become a decentralized object. At this time, the object has a database recognition value, but it is not managed by the hibernate persistent layer.
Persistent objects have the following features:
1) associated with the session instance, located in the cache of a Session instance
2) There are association records in the database.
Many methods of Session can trigger Java objects into the persistent state:
(1) The save () method of Session converts a temporary object to a persistent object.
(2) The objects returned by the load () or get () method of the Session are always in the persistent state.
(3) the List set returned by the find () method of the Session stores all persistent objects.
(4) the update (), saveOrUpdate (), and lock () Methods of the Session transform the free object into a persistent object.
(5) When A Persistent Object is associated with a temporary object and cascade storage is allowed, the Session will change the temporary object to a persistent object during cache cleaning.
3. Off-pipe status:
After the session of a persistent object is closed, the persistent object becomes a decentralized object, which cannot be synchronized with the database and is no longer managed by Hibernate. When a Session is re-associated with an unmanaged object, it is converted into a persistent object again.
The pipe dropping object has the following features:
1) it is no longer in the Session cache
2). Therefore, there may still be records corresponding to this record in the database (the premise is that no other program has deleted this record ).
The takeover object and the temporary object are the same:
The two are not associated with sessions, so Hibernate does not ensure that their attribute changes are synchronized with the database.
Differences between the unmanaged object and the temporary object:
The former is transformed from a persistent object, so there may be corresponding records in the database, while the latter has no corresponding records in the database.
When the Session close () method is called, the Session cache is cleared, and all persistent objects in the cache become free objects.If no variables are referenced in the application to reference these free objects, they end the lifecycle.
Summary:
Temporary object ----- (get, load, etc.) ------> Persistent Object ------ (close session) ------> unmanaged object
Temporary object: No session cache, no record, get, and load change to Persistent Object
Persistent Object: In session cache, there are records
Unmanaged objects: these objects are not stored in the session cache. They may have records and database IDs. They can be changed through update, saveOrUpdate, and so on.