Hibernate查询缓存

二级缓存中,如果不设置"查询缓存",那么hibernate只会缓存使用load()方法获得的单个持久化对象,如果想缓存使用findall(),list(),iterator(),createCriteria(),createQuery等方法获得的数据结果集的话,就需要设置 hibernate.cache.use_query_cache  true,即配置查询缓存
如果需要"查询缓存",还需要在使用Query和Criteria()时设置其setCacheable(true)属性

hibernate查询缓存

配置:在hibernate.cfg.xml文件中加入:<property name="hibernate.cache.use_query_cache">true</property>
	1.针对普通属性结果集的缓存
	2.对是实体对象的结果集,只缓存id
	3.使用查询缓存,需要打开查询缓存,并且在调用list方法之前需要显示的调用query.setCacheable(true);
	4.查询缓存与session无关,即在一个session上做了查询缓存,那么在另一个session中可以取到查询缓存的内容,不再发出SQL
	5.查询缓存只对query.list()操作有效,query.iterate()操作不会使用查询缓存
	6.要注意区别:一级缓存与二级缓存是缓存实体,而查询缓存是缓存普通属性.
 
package org.cric.test;

import java.util.Iterator;
import java.util.List;

import org.cric.model.Student;
import org.cric.util.HibernateUtil;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import junit.framework.TestCase;

public class QueryCacheTest extends TestCase {
	/**
	 * 执行二次query
	 */
	public void testCache1(){
		Session session = null;
		Transaction tr = null;
		try{
			session = HibernateUtil.getSession();
			tr = session.beginTransaction();
			Query query = session.createQuery("from Student");
			query.setCacheable(true);
			
			List<Object> list = query.list();
			for(Object object:list){
				Student student = (Student)object;
				System.out.println(student.getStudentName());
			}
			System.out.println("------------------------------");
			//不再发出SQL,因为启用了查询缓存.
			query = session.createQuery("from Student");
			query.setCacheable(true);
			list = query.list();
			for(Object object:list){
				Student student = (Student)object;
				System.out.println(student.getStudentName());
			}
			tr.commit();
		}catch(Exception e){
			e.printStackTrace();
			tr.rollback();
		}finally{
			HibernateUtil.closeSession(session);
		}
		
	}
	/**
	 *  执行二次query,第二个query新open一个session
	 */
	public void testCache2(){
		Session session = null;
		Transaction tr = null;
		try{
			session = HibernateUtil.getSession();
			tr = session.beginTransaction();
			Query query = session.createQuery("from Student");
			query.setCacheable(true);
			
			List<Object> list = query.list();
			for(Object object:list){
				Student student = (Student)object;
				System.out.println(student.getStudentName());
			}
			tr.commit();
		}catch(Exception e){
			e.printStackTrace();
			tr.rollback();
		}finally{
			HibernateUtil.closeSession(session);
		}
		System.out.println("---------------------------");
		//不再发出sql,因为查询缓存的生命周期和session无关
		try{
			session = HibernateUtil.getSession();
			tr = session.beginTransaction();
			Query query = session.createQuery("from Student");
			query.setCacheable(true);
			
			List<Object> list = query.list();
			for(Object object:list){
				Student student = (Student)object;
				System.out.println(student.getStudentName());
			}
			tr.commit();
		}catch(Exception e){
			e.printStackTrace();
			tr.rollback();
		}finally{
			HibernateUtil.closeSession(session);
		}
	}
	
	public void testCache3(){
		Session session = null;
		Transaction tr = null;
		try{
			session = HibernateUtil.getSession();
			tr = session.beginTransaction();
			Query query = session.createQuery("from Student");
			query.setCacheable(true);
		
			Iterator iter = query.iterate();
			while(iter.hasNext()){
				Student student = (Student)iter.next();
				System.out.println(student.getStudentName());
			}
			tr.commit();
		}catch(Exception e){
			e.printStackTrace();
			tr.rollback();
		}finally{
			HibernateUtil.closeSession(session);
		}
		System.out.println("--------------------------");
		//query.iterate()操作不会使用查询缓存
		//!!!查询缓存只对query.list()操作有效!
		try{
			session = HibernateUtil.getSession();
			tr = session.beginTransaction();
			Query query = session.createQuery("from Student");
			query.setCacheable(true);
		
			Iterator iter = query.iterate();
			while(iter.hasNext()){
				Student student = (Student)iter.next();
				System.out.println(student.getStudentName());
			}
			tr.commit();
		}catch(Exception e){
			e.printStackTrace();
			tr.rollback();
		}finally{
			HibernateUtil.closeSession(session);
		}
	}
	
}
 

相关推荐