Persisted object PO class state
1. Status classification
transient transient state: Hibernate's session cache is not stored and no data is in the database. For example, New PO class, no OID value
Persistent Persistent state: Hibernate is stored in the session cache and will eventually be in the database. For example, Save (PO) completes a commit operation with an OID value
Detached off-state: Hibernate does not have a session cache, and data is in the database. Out of session management, with OID values
2.OID
Oid:hibernate for Marking PO classes
Java Distinguished object: Address
Data-differentiated data: primary key is different
For example:
New Student (). SETSID (1)
New Student (). SETSID (1)
Consider two identical PO classes
3. Transitions between states
Instantaneous state:
Convert Persistent State: Save () | Saveorupdate () If there is an OID will execute Saveorupdate (), no OID will execute save ()
Off-state: Manually Set OID
Student stu = new Student (); Instantaneous state
STU.SETSID (1)//off-pipe, throws an exception if the OID corresponding data in the database does not exist
Persistent State:
Transient state: Execute delete () remember. The internet says: Delete State, the official did not give
Off-pipe: Evict, close, clear
Evict (): Removes the specified PO class from the session
Clear (): Clears the session cache, clears all
Close (): Session Close does not exist session cache
Off-pipe State:
Transient state: Execute delete () manually remove OID
Persistent state: Execute update (), Saveorupdate ()
4. First level cache
First -level cache: Refers to hibernate session level cache, in session cache data
Get () queries the PO class by ID, caches the results of the query to the first level cache (session)
The code is as follows
Package cn.hibernate.test;
Import org.hibernate.Session;
Import Org.hibernate.SessionFactory;
Import org.hibernate.cfg.Configuration;
Import Org.junit.Test;
Import cn.hibernate.bean.Student;
Public class Cachetest {
private Sessionfactory factory = new Configuration ()
. Configure (). addclass (Student.class)
. Buildsessionfactory ();
@Test
Public void Demo01 () {
Session session = Factory.opensession ();
//Query student object with ID 1
/*hibernate:
load Cn.hibernate.bean.Student Select
Student0_.sid as sid0_0_,
Student0_.sname as sname0_0_
from
Student student0_
where
student0_.sid=?
Student [Sid=1, sname=333]
*/
Student student= (Student) Session.get (Student.class, 1);
System.out.println (student);
}
@Test
Public void Demo02 () {
Session session = Factory.opensession ();
//Query two times ID 1 student object, supposedly produce two SQL statements, in fact, there is only one. But there are two output of the same object
/*hibernate:
load Cn.hibernate.bean.Student Select
Student0_.sid as sid0_0_,
Student0_.sname as sname0_0_
from
Student student0_
where
student0_.sid=?
Student [Sid=1, sname=333]
Student [Sid=1, sname=333]
*/
Student student= (Student) Session.get (Student.class, 1);
System.out.println (student);
//Call the evict () method to clear the student object in session to further see the resulting SQL statement
//Session.evict (student);
System.out.println ("********************************************");
/*hibernate:
load Cn.hibernate.bean.Student Select
Student0_.sid as sid0_0_,
Student0_.sname as sname0_0_
from
Student student0_
where
student0_.sid=?
Student [Sid=1, sname=333]
Hibernate:
load Cn.hibernate.bean.Student Select
Student0_.sid as sid0_0_,
Student0_.sname as sname0_0_
from
Student student0_
where
student0_.sid=?
Student [Sid=1, sname=333]
*/
Student student1= (Student) Session.get (Student.class, 1);
System.out.println (student1);
}
}
Hibernate persisted Object state