【Hibernate】——查询缓存环境配置


    查询缓存缓存什么?

    1)查询缓存是缓存普通属性结果集的

    2)对实体对象的结果集会缓存id


    查询缓存的生命周期,当关联的表发生修改,查询缓存的生命周期结束。


    查询缓存的配置和使用

    1)修改hibernate.cfg.xml文件,来开启查询缓存,默认是false是不启用的

<property name="hibernate.cache.use_query_cache">true</property>

    2)必须在程序启用

    

    示例

package com.bjpowernode.hibernate;

import java.io.Serializable;
import java.util.Iterator;
import java.util.List;

import org.hibernate.CacheMode;
import org.hibernate.Session;

import junit.framework.TestCase;

public class CacheTest extends TestCase {

	/**
	 * 开启查询,关闭二级缓存,采用query.list()查询普通属性
	 * 
	 * 在一个session中发query.list()查询
	 */
	public void testCache1() {
		Session session = null;
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			List names = session.createQuery("select s.name from Student s")
								.setCacheable(true)
								.list();
			for (int i=0; i<names.size(); i++) {
				String name = (String)names.get(i);
				System.out.println(name);
			}
			System.out.println("-------------------------------------------------------");
			//不会发出查询语句,因为启用查询缓存
			names = session.createQuery("select s.name from Student s")
							.setCacheable(true)
							.list();
			for (int i=0; i<names.size(); i++) {
				String name = (String)names.get(i);
				System.out.println(name);
			}
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally {
			HibernateUtils.closeSession(session);
		}
	}
	
	/**
	 * 开启查询,关闭二级缓存,采用query.list()查询普通属性
	 * 
	 * 在两个session中发query.list()查询
	 */
	public void testCache2() {
		Session session = null;
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			List names = session.createQuery("select s.name from Student s")
								.setCacheable(true)
								.list();
			for (int i=0; i<names.size(); i++) {
				String name = (String)names.get(i);
				System.out.println(name);
			}
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally {
			HibernateUtils.closeSession(session);
		}
		System.out.println("-------------------------------------------------------");
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			//不会发出查询语句,因为查询缓存和session的生命周期没有关系
			List names = session.createQuery("select s.name from Student s")
								.setCacheable(true)
								.list();
			for (int i=0; i<names.size(); i++) {
				String name = (String)names.get(i);
				System.out.println(name);
			}
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally {
			HibernateUtils.closeSession(session);
		}
		
	}		
	
	/**
	 * 开启查询,关闭二级缓存,采用query.iterate()查询普通属性
	 * 
	 * 在两个session中发query.iterate()查询
	 */
	public void testCache3() {
		Session session = null;
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			Iterator iter = session.createQuery("select s.name from Student s")
								.setCacheable(true)
								.iterate();
			while(iter.hasNext()) {
				String name = (String)iter.next();
				System.out.println(name);
			}
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally {
			HibernateUtils.closeSession(session);
		}
		System.out.println("-------------------------------------------------------");
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			//会发出查询语句,query.iterate()查询普通属性它不会使用查询缓存
			//查询缓存只对query.list()起作用
			Iterator iter = session.createQuery("select s.name from Student s")
							.setCacheable(true)
							.iterate();
			while(iter.hasNext()) {
				String name = (String)iter.next();
				System.out.println(name);
			}
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally {
			HibernateUtils.closeSession(session);
		}
		
	}
	
	/**
	 * 关闭查询,关闭二级缓存,采用query.list()查询实体
	 * 
	 * 在两个session中发query.list()查询
	 */
	public void testCache4() {
		Session session = null;
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			List students = session.createQuery("select s from Student s")
								.list();
			for (int i=0; i<students.size(); i++) {
				Student studnet = (Student)students.get(i);
				System.out.println(studnet.getName());
			}
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally {
			HibernateUtils.closeSession(session);
		}
		System.out.println("-------------------------------------------------------");
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			//会发出查询语句,默认query.list()每次执行都会发出查询语句
			List students = session.createQuery("select s from Student s")
								.list();
			for (int i=0; i<students.size(); i++) {
				Student studnet = (Student)students.get(i);
				System.out.println(studnet.getName());
			}
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally {
			HibernateUtils.closeSession(session);
		}
		
	}	
	
	/**
	 * 开启查询,关闭二级缓存,采用query.list()查询实体
	 * 
	 * 在两个session中发query.list()查询
	 */
	public void testCache5() {
		Session session = null;
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			List students = session.createQuery("select s from Student s")
								.setCacheable(true)
								.list();
			for (int i=0; i<students.size(); i++) {
				Student studnet = (Student)students.get(i);
				System.out.println(studnet.getName());
			}
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally {
			HibernateUtils.closeSession(session);
		}
		System.out.println("-------------------------------------------------------");
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			
			//会发出n条查询语句,因为开启了查询缓存,关闭了二级缓存,那么查询缓存就会缓存实体对象的id
			//第二次执行query.list(),将查询缓存中的id依次取出,分别到一级缓存和二级缓存中查询相应的实体
			//对象,如果存在就使用缓存中的实体对象,否则根据id发出查询学生的语句
			List students = session.createQuery("select s from Student s")
								.setCacheable(true)
								.list();
			for (int i=0; i<students.size(); i++) {
				Student studnet = (Student)students.get(i);
				System.out.println(studnet.getName());
			}
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally {
			HibernateUtils.closeSession(session);
		}
		
	}	
	
	/**
	 * 开启查询,开启二级缓存,采用query.list()查询实体
	 * 
	 * 在两个session中发query.list()查询
	 */
	public void testCache6() {
		Session session = null;
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			List students = session.createQuery("select s from Student s")
								.setCacheable(true)
								.list();
			for (int i=0; i<students.size(); i++) {
				Student studnet = (Student)students.get(i);
				System.out.println(studnet.getName());
			}
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally {
			HibernateUtils.closeSession(session);
		}
		System.out.println("-------------------------------------------------------");
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			
			//不再发出查询语句,因为配置了二级缓存和查询缓存
			List students = session.createQuery("select s from Student s")
								.setCacheable(true)
								.list();
			for (int i=0; i<students.size(); i++) {
				Student studnet = (Student)students.get(i);
				System.out.println(studnet.getName());
			}
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally {
			HibernateUtils.closeSession(session);
		}
		
	}		
}



评论 18
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值