caching: High efficiency
Hardware CPU cache memory for hard disk cache
IO Stream caching for software
Hibernate's first-level cache is also designed to operate the database efficiently.
Proof of a slow existence:
Person p=session. Get (Person.class, 1);
Person P1=session.get (person.class,2);
Person P2=session.get (person.class,3);
System.out.println (P=P1);
The console output is:
SELECT * from the person where id=1;
True
Caching principle:
Snapshot
When the code changes to:
Note: The name value of the first data in the original Database person table is "Zhang San"
Person P=session.get (person.class,1);
P.setname ("Zhangsan");
P.setname ("Zhang San");
After execution, the console appears as
SELECT * from the person where id=1;
View the database in the person table with a data ID of 1 name= "Zhang San"
This applies to the snapshot of hibernate:
Increase efficiency: 1. Improve query efficiency (caching principle) 2. Reduce unnecessary modification statement send (snapshot)
To see Hibernate object status from the cache:
1. Transient status: No ID, no in session cache
2. Persistent status: With ID, in session cache
3. Free/Managed Status: There is no ID in the cache
A deeper understanding of the snapshot: The following code intercepts some of the main code in the test method
.........
Person P=new person (); Instantaneous state
P.setid (1); Free State, with ID not cached in session
Session.update (P); Persistent state, with ID cached in session
Person P=session.get (Person.class, 1);
Tx.commit ();
.........
=new person in person P (); Set a breakpoint, debug mode to execute the view console in the SQL statement display
Until run to person p= session.get (person.class, 1); Console output update person set name=? Age=? Sex=? where id=?
This behavior occurs because the object is persisted through update, and the object is not put into a snapshot by the Get method.
When the object commits, the Nineth step is executed: The object is compared to the objects in the snapshot, and if it is not the same, the UPDATE statement is executed; The UPDATE statement is executed without a snapshot
Hibernate Learning (iv) Hibernate cache & Snapshot