Hibernate cache level:
(1) a cache is very short and the session life cycle consistent, also known as session-level cache-level cache or transaction-level cache
(2) Ways of Supporting level cache:
get ();
load ();
iterator (); only entity object cache (3) how to manage a cache
session.clear () session.evict (); session.flush ();
(4) how to avoid a large amount of data storage memory to spill sessionflush ()
In the clear ()
(5) If the amount of data in particular can be considered if the jdbc JDBC implementation can not meet the requirements of the database could be considered to provide specific data import tool

The following examples we will verify that more than 11 articles:

This has two types of

Java code

  1. public class Clazz (
  2. private Integer id;
  3. private String name;
  4. private Set <Student> students = new HashSet <Student> ();
  5. )
  6. public class Student (
  7. private Integer id;
  8. private String name;
  9. private Clazz clazz;
  10. )

public class Clazz {
   private Integer id;
   private String name;
   private Set<Student> students=new HashSet<Student>();
}
public class Student {
   private Integer id;
   private String name;
   private Clazz clazz;
}



Their mapping file are:

Xml code

  1. <class name="com.june.hibernate.Student" table="t_student">
  2. <id name="id">
  3. <generator class="native"> </ generator>
  4. </ id>
  5. <property name="name"> </ property>
  6. <many-to-one name="clazz" cascade="save-update"> </ many-to-one>
  7. </ class>
  8. <class name="com.june.hibernate.Clazz" table="t_clazz">
  9. <id name="id">
  10. <generator class="native"> </ generator>
  11. </ id>
  12. <property name="name"> </ property>
  13. <set name="students" inverse="true">
  14. <key column="clazz" />
  15. <one-to-many class="com.june.hibernate.Student" />
  16. </ set>
  17. </ class>

   <class name="com.june.hibernate.Student" table="t_student">
	   <id name="id">
	       <generator></generator>
	   </id>
	   <property name="name"></property>	
            <many-to-one name="clazz" cascade="save-update"></many-to-one> 	
   </class>

<class name="com.june.hibernate.Clazz" table="t_clazz">
	   <id name="id">
	       <generator></generator>
	   </id>
	   <property name="name"></property>	
	   <set name="students" inverse="true">
	      <key column="clazz"/>
	      <one-to-many/>
	   </set> 
</class>
 


Please run the following test methods of all:
Java code:



package com.june.hibernate;

import java.io.Serializable;

import junit.framework.TestCase;

import org.hibernate.Session;
import org.hibernate.Transaction;

public class CacheLevel1Test extends TestCase (
/ **
* In the same session, 2 times the implementation of load () queries * /
public void testLoad () (
Session session = null;
Transaction tx = null;
try (
session = HibernateUtil.getSession ();
tx = session.beginTransaction ();
Student student1 = (Student) session.load (Student.class, 1);
System.out.println ( "student1.name =" + student1.getName ());
System.out.println ( "stuent1.clazz.name" + student1.getClazz (). GetName ());
/ / Sql statement will not be issued because the load using the cache Student student2 = (Student) session.load (Student.class, 1);
System.out.println ( "student1.name =" + student1.getName ());
tx.commit ();
) catch (Exception e) (
e.printStackTrace ();
tx.rollback ();
) finally (
HibernateUtil.colseSession (session);
)
)
/ **
* In the same session, 2 times the implementation of get () query * /
public void testGet () (
Session session = null;
Transaction tx = null;
try (
session = HibernateUtil.getSession ();
tx = session.beginTransaction ();
Student student1 = (Student) session.get (Student.class, 1);
System.out.println ( "student1.name =" + student1.getName ());
System.out.println ( "stuent1.clazz.name" + student1.getClazz (). GetName ());
/ / Will not send sql statement, get the use of cache because Student student2 = (Student) session.get (Student.class, 1);
System.out.println ( "student1.name =" + student1.getName ());
tx.commit ();
) catch (Exception e) (
e.printStackTrace ();
tx.rollback ();
) finally (
HibernateUtil.colseSession (session);
)
)
/ **
* Inquiries entities * /
public void testIteratorEntity () (
Session session = null;
Transaction tx = null;
try (
session = HibernateUtil.getSession ();
tx = session.beginTransaction ();
Student student1 = (Student) session.createQuery ( "from Student s where s.clazz.id = 1"). Iterate (). Next ();
System.out.println ( "student1.name =" + student1.getName ());
/ ** Hibernate: select student0_.id as col_0_0_ from t_student student0_ where student0_.clazz = 1
Hibernate: select student0_.id as id1_0_, student0_.name as name1_0_, student0_.clazz as clazz1_0_ from t_student student0_ where student0_.id =?
student1.name = Student_ ** 0Of_Clazz000
Hibernate: select student0_.id as col_0_0_ from t_student student0_ where student0_.clazz = 1
student2.name = Student_ ** 0Of_Clazz000
* /
/ / Cache id = 1 already exists so the student is only responsible for issuing the second query the sql query will not be issued id entity sql query
/ / Iterator to use the cache because Student student2 = (Student) session.createQuery ( "from Student s where s.clazz.id = 1"). Iterate (). Next ();
System.out.println ( "student2.name =" + student2.getName ());
tx.commit ();
) catch (Exception e) (
e.printStackTrace ();
tx.rollback ();
) finally (
HibernateUtil.colseSession (session);
)
)
/ **
* Inquiries ordinary property * /
public void testIteratorProperty () (
Session session = null;
Transaction tx = null;
try (
session = HibernateUtil.getSession ();
tx = session.beginTransaction ();
String sname = (String) session.createQuery ( "select s.name from Student s where s.clazz.id = 1"). Iterate (). Next ();
System.out.println ( "student1.name =" + sname);
/ / iterator inquiries ordinary property, a cache will not be cached, so it will not issue a sql statement String sname2 = (String) session.createQuery ( "select s.name from Student s where s.clazz.id = 1") . iterate (). next ();
System.out.println ( "student1.name =" + sname2);
tx.commit ();
) catch (Exception e) (
e.printStackTrace ();
tx.rollback ();
) finally (
HibernateUtil.colseSession (session);
)
)
/ **
* 2 session in the use of load
* /
public void testTwoSessionLoad () (
Session session = null;
Transaction tx = null;
try (
session = HibernateUtil.getSession ();
tx = session.beginTransaction ();
Student student1 = (Student) session.load (Student.class, 1);
System.out.println ( "student1.name =" + student1.getName ());
tx.commit ();
) catch (Exception e) (
e.printStackTrace ();
tx.rollback ();
) finally (
HibernateUtil.colseSession (session);
)

try (
session = HibernateUtil.getSession ();
tx = session.beginTransaction ();
/ / Sql statement will be issued will not be shared inter-session level cache / / he will accompany the life cycle of this session of the existence and disappearance of Student student1 = (Student) session.load (Student.class, 1);
System.out.println ( "student1.name =" + student1.getName ());
tx.commit ();
) catch (Exception e) (
e.printStackTrace ();
tx.rollback ();
) finally (
HibernateUtil.colseSession (session);
)
)
/ **
* At the same session in a load () query save () the data * /
public void testLoadSaved () (
Session session = null;
Transaction tx = null;
try (
session = HibernateUtil.getSession ();
tx = session.beginTransaction ();
Student student = new Student ();
student.setName ( "mary");
Serializable

/ / Will not issue a sql, because the use of save cache Student student1 = (Student) session.load (Student.class, id);
System.out.println (student1.getName ());

tx.commit ();
) catch (Exception e) (
e.printStackTrace ();
tx.rollback ();
) finally (
HibernateUtil.colseSession (session);
)
)
/ **
* In the first session with a call load then the implementation of inquiry session.clear () or the implementation of session.evict ();
* In the call load query * clear () clear the cache, get rid of all * evict (Object object) expelled from the cache specified object * /
public void testLoadEvictLoad () (
Session session = null;
Transaction tx = null;
try (
session = HibernateUtil.getSession ();
tx = session.beginTransaction ();
Student student1 = (Student) session.load (Student.class, 1);
/ / Sql statement issued System.out.println ( "student1.name =" + student1.getName ());
/ / A cache can not cancel but can manage, such as session.clear () session.evict ()
/ / Clear the cache session.clear ();
/ / session.evict (student1); / / evicted from the cache id = 1 of the student

Student student2 = (Student) session.load (Student.class, 1);
/ / Sql statement will be issued System.out.println ( "student1.name =" + student1.getName ());
tx.commit ();
) catch (Exception e) (
e.printStackTrace ();
tx.rollback ();
) finally (
HibernateUtil.colseSession (session);
)
)
/ **
* Add to the database data 10000 * /
public void testCache7 () (
Session session = null;
Transaction tx = null;
long beginTime = System.currentTimeMillis ();
try (
session = HibernateUtil.getSession ();
tx = session.beginTransaction ();
for (int i = 0; i <1000; i + +) (
Student student = new Student ();
student.setName ( "student_" + i);
session.save (student);
/ / 20 data for each insert on the mandatory data persistence / / clear the cache at the same time to avoid a large amount of data caused by memory overflow if (i% 20 == 0) (
session.flush ();
session.clear ();
)
)
System.out.println ();
tx.commit ();
) catch (Exception e) (
e.printStackTrace ();
tx.rollback ();
) finally (
HibernateUtil.colseSession (session);
)
long endTime = System.currentTimeMillis ();
System.out.println (endTime-beginTime);
)
)