Hibernate-level caching, also known as sesssion cache
The first-level cache lifecycle is short and the lifecycle of the session is consistent, and the primary cache is called the session-level cache or transaction-level cache. is primarily used to cache entity objects without caching the normal properties of entity objects
Methods to support first-level caching:
* GET ()
* Load ()
* Iterate (query entity objects)
How to manage the first-level caching:
* Session.clear (), Session.evict ()
How to avoid a one-time large amount of Entity data warehousing results in memory overflow
* Flush First, then clear
If the volume of data is particularly large, consider a JDBC implementation, and if JDBC does not meet the requirements, consider adopting a specific import tool for the data itself
test Case (the test case is based on a One-to-many bidirectional association map):
Package com.bjsxt.hibernate;
Import java.io.Serializable;
Import org.hibernate.Session;
Import Junit.framework.TestCase;
public class Cachelevel1test extends TestCase {
/**
* Two load queries issued in the same session
*/
public void TestCache1 () {
Session session = NULL;
try {
Session = Hibernateutils.getsession ();
Session.begintransaction ();
Student Student = (Student) session.load (Student.class, 1);
System.out.println ("student.name=" + student.getname ());
does not emit SQL because the load uses caching
Student = (student) session.load (Student.class, 1);
System.out.println ("student.name=" + student.getname ());
Session.gettransaction (). commit ();
}catch (Exception e) {
E.printstacktrace ();
Session.gettransaction (). rollback ();
}finally {
Hibernateutils.closesession (session);
}
}
/**
* Send two get queries in the same session
*/
public void TestCache2 () {
Session session = NULL;
try {
Session = Hibernateutils.getsession ();
Session.begintransaction ();
Student Student = (Student) session.get (Student.class, 1);
System.out.println ("student.name=" + student.getname ());
SQL is not emitted because get uses caching
Student = (student) Session.get (Student.class, 1);
System.out.println ("student.name=" + student.getname ());
Session.gettransaction (). commit ();
}catch (Exception e) {
E.printstacktrace ();
Session.gettransaction (). rollback ();
}finally {
Hibernateutils.closesession (session);
}
}
/**
* Two iterate query entity objects in the same session
*/
public void TestCache3 () {
Session session = NULL;
try {
Session = Hibernateutils.getsession ();
Session.begintransaction ();
Student Student = (Student) session.createquery ("from Student s where s.id=1"). Iterate (). Next ();
System.out.println ("student.name=" + student.getname ());
SQL that emits the query ID and does not emit SQL for querying entity objects because iterate uses the cache
Student = (student) session.createquery ("from student s where s.id=1"). Iterate (). Next ();
System.out.println ("student.name=" + student.getname ());
Session.gettransaction (). commit ();
}catch (Exception e) {
E.printstacktrace ();
Session.gettransaction (). rollback ();
&