Hibernate cache and what operations will read and hold data to the cache
Hibernate Cache
Hibernate has a level two cache, one level cache and one level two cache, respectively. The first-level cache is also called session-level caching, which is available by default, without configuration. The primary cache life cycle is determined by the session object, the session object is closed, and the first level cache disappears. A secondary cache is also called a sessionfactory-level cache, which needs to be configured before it can be used. The life cycle of a secondary cache is longer than the life cycle of a primary cache, which is determined by the Sessionfactory object, the Sessionfactory object is closed, and the level two cache disappears. which operations read and hold data to the cache.
Before the test to say hibernate access to the database, hibernate to the traditional JDBC encapsulation, generally we access the database is nothing more than add, delete, change, check the four operations, and these four operations are completed through their own SQL statements, So hibernate on the database to do these four operations can not leave the SQL statement, if we configure hibernate configuration Show_sql This attribute, once hibernate to the database has been added, deleted, changed, check operation, The console will print out the executed SQL statements, which is why we can have the console have no print SQL statements as hibernate has no access to the database. Start the test below:
Test environment:
System: Windows 10
jdk:1.8.102
hibernate:5.2.10
Software: MyEclipse 2016
Let's look at the query and query our common list () method with Get (), load (), and the Query object in HQL (later for convenience, we call Query.list ()) and the Uniqueresult () method (which is handy, We call these methods Query.uniqueresult ()).
The results first:
get (), load () will store and read data to the cache, while Query.list () and Query.uniqueresult () will store the data in the cache without reading the data from the cache
get () method:
Before testing the Get () method is how to find the data, it will go to the first cache to find, no, it will go to the level two cache to continue to find, the level two cache is not yet, will immediately generate the corresponding query SQL, and send this SQL to the database, to the database to find the number 7499 employees , NULL is returned if it is not found in the database. If anything in the database returns the result of the query, it will also put the results in the cache (level one or two cache will be put) while it returns the results of the query.
Test the Get () method code:
Get () stores and reads data to the cache public
static void Testgetcache (Session session) {
try {
Employee emp6=session.get ( Employee.class, 7499);
System.out.println (EMP6);
Employee Emp7=session.get (Employee.class, 7499);
System.out.println (EMP7);
} catch (Exception e) {
e.printstacktrace ();
} finally{
session.close ();
}
}
Operation Result:
Hibernate:
Select
employee0_.empno as empno1_0_0_,
employee0_.ename as ename2_0_0_,
employee0_.job As job3_0_0_,
employee0_.sal as sal4_0_0_,
Employee0_.deptno as deptno5_0_0_,
employee0_.hiredate as hiredate6_0_0_,
Employee0_.comm as comm7_0_0_,
employee0_.mgr as mgr8_0_0_
from
SCOTT. EMP employee0_
where
employee0_.empno=?
Employee Name: Allen Employee number 7499
employee Name: Allen Employee Number 7499
The code in our query number 7499 employees checked 2 times, but the results of the operation only printed the first query SQL statement, the first time is to check the database, the second did not go to the database, but the second time still found the number 7499 employees, because of hibernate cache mechanism, The first time to find the number 7499 employees will put 7499 of the employee information into the cache, when we re-find the second time the 7499 employee information, because in the cache has been found, so directly return the results, will not go to the database to check, so the console does not print the second SQL statement. This means that the second query is checked (read) from the cache. So the result is obvious: get () stores and reads data into the cache.
get () test result: Get () will store and read data to the cache
load () method:
Load () and get () look for data in the same way, but with a little difference. Load () is also first-level cache to find, did not find the words continue to the level two cache, the level two cache has not been found, it will return a query result of the proxy object, when we use the query results later, this time it will generate the corresponding SQL statement, and send SQL to the database, To the database to find, if you do not use the query results, it will not generate query SQL, not to the database to find. In other words, when you use the query results, he will go to the database to find, if not found in the database, will error, will report org.hibernate.ObjectNotFoundException exception, resulting in a program interruption. If anything in the database returns the result of the query, it will also put the results in the cache (level one or two cache will be put) while it returns the results of the query.
Test the Load () method code:
Load () stores and reads data to the cache public
static void Testloadcache (Session session) {
try {
Employee emp6= Session.get (Employee.class, 7369);
System.out.println (EMP6);
Employee Emp7=session.get (Employee.class, 7369);
System.out.println (EMP7);
} catch (Exception e) {
e.printstacktrace ();
} finally{
session.close ();
}
}
Operation Result:
Hibernate:
Select
employee0_.empno as empno1_0_0_,
employee0_.ename as ename2_0_0_,
employee0_.job As job3_0_0_,
employee0_.sal as sal4_0_0_,
Employee0_.deptno as deptno5_0_0_,
employee0_.hiredate as hiredate6_0_0_,
Employee0_.comm as comm7_0_0_,
employee0_.mgr as mgr8_0_0_
from
SCOTT. EMP employee0_
where
employee0_.empno=?
Employee Name: Smith Employee number 7369
Employee Name: Smith Employee Number 7369
In the code we also query number 7369 employees checked 2 times, but the results of the operation only printed the first query of the SQL statement, the first time is to check the database, the second did not go to the database, but the second time still found the number 7369 employees, The reason is also because Hibernate caching mechanism, the first time to find the number 7369 employees will put 7369 of the employee information into the cache, the second search of 7369 of the employee information is from the cache (read) out. So load () also stores and reads data into the cache.
load () method test Result: Load () will store and read data to the cache
query.list () method
Test the Query.list () method code:
Test Query.list () stores data in the cache but does not read data from the cache public static void Testquerylistcache (session session) {try {
String hql= "from Employee";
Query query=session.createquery (HQL);
List<employee> list=query.list (); for (int i=0;i<2;i++) {System.out.println ("Employee Number:" +list.get (i). Getempno () + "Employee Name:" +list.get (i). getemp
Name ());
} System.out.println ("------------------------------------------");
Employee emp= (Employee) Session.get (Employee.class, 7654);
SYSTEM.OUT.PRINTLN ("Employee Number:" +emp.getempno () + "Employee Name:" +emp.getempname ());
System.out.println ("------------------------------------------");
String hql2= "from Employee";
Query query2=session.createquery (HQL2);
List<employee> list2=query2.list (); for (int i=0;i<2;i++) {System.out.println ("Employee Number:" +list2.get (i). Getempno () + "Employee Name:" +list2.get (i). Getempname ());
}} catch (Exception e) {e.printstacktrace ();
}finally{Session.close (); }
}
Operation Result:
Hibernate:
Select
employee0_.empno as empno1_0_,
employee0_.ename as ename2_0_,
Employee0_.job as job3_0_,
employee0_.sal as sal4_0_,
Employee0_.deptno as deptno5_0_, employee0_.hiredate as
hiredate6_0_ ,
Employee0_.comm as comm7_0_,
employee0_.mgr as mgr8_0_
from
SCOTT. EMP employee0_
Employee ID: Employee Name: EMPSU2 Employee
Number: + employee Name: EmpSave2
----------------------------- -------------
Employee ID: 7654 staff Name: MARTIN
------------------------------------------
Hibernate:
Select
employee0_.empno as empno1_0_,
employee0_.ename as ename2_0_,
employee0_.job as job3_0_,
employee0_.sal as sal4_0_,
Employee0_.deptno as deptno5_0_,
employee0_.hiredate as hiredate6_0_,
Employee0_.comm as comm7_0_,
employee0_.mgr as mgr8_0_
from
SCOTT. EMP employee0_
Employee ID: Employee Name: EMPSU2 Employee
Number: + Employee Name: empSave2
In the test code we use the Query.list () method to query the number and name of all employees two times (for the sake of viewing the results, the cycle of the employee's number and name is only 2 cycles), respectively, before the first dashed line and the second dashed line. Between two dashed lines we used get () to find one of the employees, look at the two dashed line there is no print get () query the employee's SQL statement to verify the first query.list () query, there is no data stored in the cache.
The result of the operation shows that the console has printed out the SQL statements for all employees in the two query.list () query, stating that two times query.list () queries are found in the database. Two times query.list () query we used get () to query the employee number 7654, the results show that we found the employee's information, but the console did not print the SQL statement to query the employee information, the get () did not go to the database to check, get () The employee information found in number 7654 is not found in the database, because get () the order of finding data is first found in the cache and then to the database, but we use get () to find the employee number 7654, indicating that get () data is found from the cache (read), Observing the entire test code, get () query before we only carried out the query.list () query operation, so it is clear that the information in the cache is query.list () after the results are put in, which means that query.list () will store the data in the cache, All the employee information is already in the cache, but the query.list () query operation after get () is still in the database when it is already in the cache, so it is found that query.list () does not look up (read) data from the cache. Comprehensive: Test query.list () stores data in the cache but does not read data from the cache
query.list () test Result: Query.list () will store the data in the cache but not read the data from the cache
Query.uniqueresult () method:
Test the Query.uniqueresult () code:
Test Query.uniqueresult () will store the data in the cache but will not read the data from the cache public static void Testqueryuniqueresultcache (session session) {
try {String hql= "from Employee where empno=7876";
Query query=session.createquery (HQL);
Employee emp= (Employee) Query.uniqueresult ();
SYSTEM.OUT.PRINTLN ("Employee Number:" +emp.getempno () + "Employee Name:" +emp.getempname ());
System.out.println ("-------------------------------------------------");
Test query.list () with Get () there is no data stored in the cache employee emp3= (employee) Session.get (Employee.class, 7876);
SYSTEM.OUT.PRINTLN ("Employee Number:" +emp3.getempno () + "Employee Name:" +emp3.getempname ());
System.out.println ("-------------------------------------------------");
String hql2= "from Employee where empno=7876";
Query query2=session.createquery (HQL2);
Employee emp2= (Employee) Query.uniqueresult (); SYSTEM.OUT.PRINTLN ("Employee Number:" +emp2.getempno () + "Employee Name:" +emp2.getempName ());
} catch (Exception e) {e.printstacktrace ();
}finally{Session.close (); }
}
Run Result:
Hibernate:select Employee0_.empno as empno1_0_, employee0_.ename as ename2_0_, Employee0_.jo B as job3_0_, employee0_.sal as sal4_0_, Employee0_.deptno as deptno5_0_, employee0_.hiredate as H iredate6_0_, Employee0_.comm as comm7_0_, employee0_.mgr as mgr8_0_ from SCOTT. EMP employee0_ where employee0_.empno=7876 employee number: 7876 Employee Name: ADAMS------------------------------------------- ------Employee ID: 7876 Employee Name: ADAMS-------------------------------------------------hibernate:select employee0_. Empno as empno1_0_, employee0_.ename as ename2_0_, employee0_.job as job3_0_, employee0_.sal as SA l4_0_, Employee0_.deptno as deptno5_0_, employee0_.hiredate as hiredate6_0_, Employee0_.comm as Co mm7_0_, employee0_.mgr as mgr8_0_ from SCOTT. EMP employee0_ where employee0_.empno=7876 employee number: 7876 Employee Name: ADAMS
Similar to the above Test query.list (), we used Query.uniqueresult () to check the number 7876 employees two times, respectively, before the first dashed line and the second dashed line. In the middle of the two dashed lines we use get () to find the employee of number 7876 again, and see if there is a SQL statement between the two dashed lines that prints the Get () query to determine if Query.uniqueresult () has put data into the cache.
The results show that there are two dashed lines in the print query SQL, two times Query.uniqueresult () query is to the database to check, from the running results, there is no print SQL statement between the two dashed line, we use Get () Find employees 7876 when not to the database to check, but still found the employee information, according to get () to find the data in the order of the Get () and did not go to the database to check, you can know, get () to check the employee information is from the cache (read) to the, still is to observe the entire test code, get () query the employee in front of the Query.uniqueresult () in addition to the query operation does not do anything else, so, it is clear that the information in the cache is Query.uniqueresult () before the results are put in. This means that Query.uniqueresult () will store the data in the cache.
Again, in conjunction with the Query.uniqueresult () query operation behind get (), when the employee in the cache already has a lookup, it is still in the database to find. So it concludes that Query.uniqueresult () does not read data from the cache.
Synthesis is: Query.uniqueresult () will store the data in the cache but will not read the data from the cache
test Result: Query.uniqueresult () stores data in the cache but does not read data from the cache
After we say the query, we say add, add our common have Save () and Saveorupdate ()
The results first:
when Save () and saveorupdate () are added, the data is stored in the cache.
Save () method:
Test Save () method code
Test Save () stores the data in the cache public
static void Testsavecache (Session session) {
try {
Transaction tr= Session.begintransaction ();
Employee Emp1=new employee ();
Emp1.setempname ("EmpSave2");
Session.save (EMP1);
System.out.println ("The primary key of the added object is:" +emp1.getempno ());
Employee emp2= (Employee) Session.get (Employee.class,emp1.getempno ());
SYSTEM.OUT.PRINTLN ("Employee Name:" +emp2.getempname ());
Tr.commit ()///The SQL
} catch (Exception e) {e.printstacktrace () is sent to the database only when this code executes
;
} finally{
session.close ();
}
}
Operation Result:
Hibernate:
Select
hibernate_sequence.nextval
the
primary key for the object added from dual is: +
employee Name: EmpSave2
Hibernate:
insert
into
SCOTT. Emp
(ename, Job, Sal, Deptno, HireDate, comm, Mgr, empno)
values
(?,?,?,?,?,?,?,?)
As can be seen from the code, we are in Tr.commit (); Before this code we use Get () to query the currently added object, at this time Tr.commit (), has not been executed, that is, the transaction has not been committed, so the object has not yet been stored in the database, supposedly we should not find records.
But from the results of the operation, we can see that the data, and the console does not print the relevant query SQL, that we use get () to find the employee information is not from the database to find, combined with get () to find the data is the first from the cache to the database to find the order, so we can conclude that The data found is from the cache, and there is no other action except the Save () operation before our get () query, so the data in the cache is put in Save (), which means that when we call Save () it will put the added information into the cache.
Save () test Result: Save () will store the data in the cache
saveorupdate () method:
Test Saveorupdate () method code
When the test saveorupdate () is added, the data is stored to the cache public
static void Testsaveorupdatecache (Session session) {
try {
Transaction tx=session.begintransaction ();
Employee Emp3=new employee ();
Emp3.setempname ("EmpSu2");
Session.saveorupdate (Emp3);
System.out.println ("The primary key of the added object is:" +emp3.getempno ());
Employee emp4= (Employee) Session.get (Employee.class,emp3.getempno ());
SYSTEM.OUT.PRINTLN ("Employee Name:" +emp4.getempname ());
Tx.commit ()///The SQL
} catch (Exception e) {e.printstacktrace () is sent to the database only when this code executes
;
} finally{
session.close ();
}
}
Operation Result:
Hibernate:
Select
hibernate_sequence.nextval
the
primary key for the object added from dual is:
Employee Name: EMPSU2
Hibernate:
insert
into
SCOTT. Emp
(ename, Job, Sal, Deptno, HireDate, comm, Mgr, empno)
values
(?,?,?,?,?,?,?,?)
and test Save (), we are in Tx.commit (); Before this code we use Get () to query the currently added object, at this time Tr.commit (), has not been executed, that is, the transaction has not been committed, so the object has not yet been stored in the database, supposedly we should not find the record.
The result is also the same as the above save (), the console does not print the relevant query SQL instructions, indicating that we use get () to find the employee information is not from the database to find, combined with get () to find the data is first from the cache to the database to find the order, so we can conclude that The data found is from the cache, and there is no other action except the saveorupdate () operation before our get () query, so the information in the cache is saveorupdate () operation, so Saveorupdate () The data is also stored in the cache.
saveorupdate () test Result: Saveorupdate () will store the data in the cache
The above are personal understanding, if there are errors, please correct