hibernate缓存分:一级缓存、二级缓存、三级缓存

一级缓存:Session内的缓存

实例:

/*一级缓存:
* session内的缓存
* */
@Test
public void test1(){ /*使用缓存之前,2条结果*/
Session session =HibernateSessionFactory.getSession();
//第一次查询
Emp emp = (Emp) session.load(Emp.class, (short)7369);
System.out.println(emp.getEname()+"\t"+emp.getDept().getDname()); session.close();
//同一个session内,再次获取ID为7639的员工
Emp emp2 = (Emp) session.load(Emp.class, (short)7369);
System.out.println(emp2.getEname()+"\t"+emp2.getDept().getDname()); }

二级缓存:

进程 或集群范围内的缓存。可以被所有session共享,生活周期与SessionFactory一样,
* 二级缓存是可配置的插件,hibernate打包一些开源缓存实现,提供对它们的内置支持

用到的jar包和ehcache.xml

hibernate.cfg.xml配置:

<!--二级缓存配置开始====  -->
<property name="cache.use_second_level_cache">true</property>
<property name="cache.provider_class">
org.hibernate.cache.EhCacheProvider
</property>

实体类映射文件Emp.hbm.xml中配置:

 <!--  实体类中二级缓存配置-->
<cache usage="read-write"/>

测试代码:

/*二级缓存:
* 进程 或集群范围内的缓存。可以被所有session共享,生活周期与SessionFactory一样,
* 二级缓存是可配置的插件,hibernate打包一些开源缓存实现,提供对它们的内置支持
* */ @Test
public void test2(){
Session session=HibernateSessionFactory.getSession(); try {
Emp emp = (Emp) session.load(Emp.class, (short)7369);
System.out.println(emp.getEname()+"\t"+emp.getSal());
session.close();
Session session2=HibernateSessionFactory.getSession();
Emp emp2 = (Emp) session2.load(Emp.class, (short)7369);
System.out.println(emp2.getEname()+"\t"+emp2.getSal());
session2.close();
} catch (Exception e) {
e.printStackTrace();
}
}

三级缓存,查询缓存:

hibernate.cfg.xml中配置,所有配置均在二级缓存配置之上加入:

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

测试代码:

/*query查询缓存*/
@Test
public void test3(){
Session session=HibernateSessionFactory.getSession();
Query query=session.createQuery("from Emp where empno=7369");
Emp emp = (Emp) query.setCacheable(true).uniqueResult();
System.out.println(emp.getEname()+"\t"+emp.getSal()); session.close();
//在不同的session范围内,现次获取ID=7369的学生信息
Session session2=HibernateSessionFactory.getSession();
Query query2=session.createQuery("from Emp where empno=7369");
Emp emp2 = (Emp) query.setCacheable(true).uniqueResult();
System.out.println(emp2.getEname()+"\t"+emp2.getSal());
}

链接:http://pan.baidu.com/s/1geVPuoV 密码:s3sh

hibernate-cache的更多相关文章

  1. org.hibernate.HibernateException: could not instantiate RegionFactory [org.hibernate.cache.impl.bridge.RegionFactoryCacheProviderBridge]

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFacto ...

  2. Invocation of init method failed; nested exception is org.hibernate.HibernateException: could not instantiate RegionFactory [org.hibernate.cache.impl

    严重: Exception sending context initialized event to listener instance of class org.springframework.we ...

  3. Unable to create requested service org.hibernate.cache.spi.RegionFactory

    hibernate 4.3.11+struts2.3.28.1+spring 4.2.5,在搭框架的时候,报的这个错误: Unable to create requested service org. ...

  4. Caused by: org.hibernate.boot.registry.selector.spi.StrategySelectionException: Unable to resolve name [org.hibernate.cache.ehcache.EhCacheRegionFactory] as strategy [org.hibernate.cache.spi.RegionFac

    警告: Exception encountered during context initialization - cancelling refresh attempt: org.springfram ...

  5. Hibernate缓存之Aop+cache

    在上一篇涉及到查询缓存的功能时除了需要在配置文件中开启缓存外,还需要在业务代码中显示调用setCacheable(boolean)才可以打开查询缓存的功能,这样做,无疑是破坏了封装性,所以就诞生了利用 ...

  6. ehcache-----在spring和hibernate下管理ehcache和query cache

    1. 在Hibernate配置文件中设置: <!-- Hibernate SessionFactory --> <bean id="sessionFactory" ...

  7. Maven搭建SpringMVC+Hibernate项目详解 【转】

    前言 今天复习一下SpringMVC+Hibernate的搭建,本来想着将Spring-Security权限控制框架也映入其中的,但是发现内容太多了,Spring-Security的就留在下一篇吧,这 ...

  8. Struts2+Spring+Hibernate框架整合总结详细教程

    一.SSH三大框架知识总结 Struts 2是Struts的下一代产品,是在 struts 1和WebWork的技术基础上进行了合并的全新的Struts 2框架.其全新的Struts 2的体系结构与S ...

  9. Java三大框架之——Hibernate中的三种数据持久状态和缓存机制

    Hibernate中的三种状态   瞬时状态:刚创建的对象还没有被Session持久化.缓存中不存在这个对象的数据并且数据库中没有这个对象对应的数据为瞬时状态这个时候是没有OID. 持久状态:对象经过 ...

  10. Hibernate关联映射 映射文件的配置

    一:多对一单向关联 首先我们必须创建两个实体类 例如:Dept类 public class Dept { private Integer deptNo; private String dName; p ...

随机推荐

  1. MyBatis Generator作为maven插件自动生成增删改查代码及配置文件例子

    什么是MyBatis Generator MyBatis Generator (MBG) 是一个Mybatis的代码生成器,可以自动生成一些简单的CRUD(插入,查询,更新,删除)操作代码,model ...

  2. SqlServer环境配置和卸载

    一.数据库简介 SQLServer环境配置 安装好数据库以后怎么启用sa账号,来访问数据库. 1.先用windows账号登录数据库. 2.启用windows身份验证方式和sql server身份验证方 ...

  3. SQL SERVER普通用户需要什么权限才能执行sp_configure命令

    SQL SERVER普通用户需要什么权限才能执行sp_configure命令呢? 例如如下存储过程所示 CREATE PROCEDURE PRC_TEST AS BEGIN    exec sp_co ...

  4. MongoDB学习笔记~自己封装的Curd操作(按需更新的先决条件)

    回到目录 我们上一讲中介绍了大叔封装的Mongo仓储,其中介绍了几个不错的curd操作,而对于按需更新内部子对象,它是有条件的,即你的子对象不能为null,也就是说,我们在建立主对象时,应该为子对象赋 ...

  5. Hadoop 之 MapReduce 框架演变详解

    经典版的MapReduce 所谓的经典版本的MapReduce框架,也是Hadoop第一版成熟的商用框架,简单易用是它的特点,来看一幅图架构图: 上面的这幅图我们暂且可以称谓Hadoop的V1.0版本 ...

  6. HDFS Federation (读书笔记)

    HDFS Federation (读书笔记) HDFS的架构 HDFS包含两个层次:命名空间管理(Namespace) 和 块/存储管理(Block Storage). 命名空间管理(Namespac ...

  7. 一些常用的String方法 C#

    String Reference: https://msdn.microsoft.com/en-us/library/system.string(v=vs.110).aspx Method Strin ...

  8. OpenStack 企业私有云的若干需求(6):大规模扩展性支持

    本系列会介绍OpenStack 企业私有云的几个需求: 自动扩展(Auto-scaling)支持 多租户和租户隔离 (multi-tenancy and tenancy isolation) 混合云( ...

  9. hdu[1711]number sequence

    Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], .... ...

  10. 使用codemodel生成Java代码

    例子 JCodeModel cm = new JCodeModel(); JDefinedClass dc = cm._class("foo.Bar"); dc.annotate( ...