hibernate-cache
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的更多相关文章
- org.hibernate.HibernateException: could not instantiate RegionFactory [org.hibernate.cache.impl.bridge.RegionFactoryCacheProviderBridge]
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFacto ...
- 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 ...
- 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. ...
- 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 ...
- Hibernate缓存之Aop+cache
在上一篇涉及到查询缓存的功能时除了需要在配置文件中开启缓存外,还需要在业务代码中显示调用setCacheable(boolean)才可以打开查询缓存的功能,这样做,无疑是破坏了封装性,所以就诞生了利用 ...
- ehcache-----在spring和hibernate下管理ehcache和query cache
1. 在Hibernate配置文件中设置: <!-- Hibernate SessionFactory --> <bean id="sessionFactory" ...
- Maven搭建SpringMVC+Hibernate项目详解 【转】
前言 今天复习一下SpringMVC+Hibernate的搭建,本来想着将Spring-Security权限控制框架也映入其中的,但是发现内容太多了,Spring-Security的就留在下一篇吧,这 ...
- Struts2+Spring+Hibernate框架整合总结详细教程
一.SSH三大框架知识总结 Struts 2是Struts的下一代产品,是在 struts 1和WebWork的技术基础上进行了合并的全新的Struts 2框架.其全新的Struts 2的体系结构与S ...
- Java三大框架之——Hibernate中的三种数据持久状态和缓存机制
Hibernate中的三种状态 瞬时状态:刚创建的对象还没有被Session持久化.缓存中不存在这个对象的数据并且数据库中没有这个对象对应的数据为瞬时状态这个时候是没有OID. 持久状态:对象经过 ...
- Hibernate关联映射 映射文件的配置
一:多对一单向关联 首先我们必须创建两个实体类 例如:Dept类 public class Dept { private Integer deptNo; private String dName; p ...
随机推荐
- 使用multi-paxos实现日志同步应用
paxos 说multi-paxos之前先简要说一下paxos paxos是在多个成员之间对某个值(提议)达成一致的一致性协议.这个值可以是任何东西.比如多个成员之间进行选主,那么这个值就是主的身份. ...
- 烂泥:智能DNS使用与配置
本文由ilanniweb提供友情赞助,首发于烂泥行天下 想要获得更多的文章,可以关注我的微信ilanniweb 公司的业务现在已经扩展到海外,对外提供的统一接口都是通过域名来解析的,但是海外用户访问国 ...
- WPF 无边框透明按钮
在实际开发过程中,有时候要设置一个无边框的按钮,或者无边框的透明按钮. 按钮效果如下: 1.当你应用telerik组件中的Button时,这个直接就可以设置 telerik:StyleManager. ...
- 常见博客API
新浪博客 http://upload.move.blog.sina.com.cn/blog_rebuild/blog/xmlrpc.php 网易博客 http://os.blog.163.com/ap ...
- WIN7下django1.8下载安装
前言:公司电脑上django是在pycharm上下载自动安装的,家里电脑没安pycharm,所以自己手动安装. django下载地址:https://www.djangoproject.com/dow ...
- CODEVS3037 线段覆盖 5[序列DP 二分]
3037 线段覆盖 5 时间限制: 3 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题解 题目描述 Description 数轴上有n条线段,线段的 ...
- java程序设计线程池(newCachedThreadPool())
创建一个无界的可缓存的线程池,若线程长时间没用会自动销毁,直接上代码好了: import java.util.concurrent.ExecutorService; import java.util. ...
- 解决 uuid.h找不到的问题
http://blog.csdn.net/commshare/article/details/40835407
- Python-07-面向对象(进阶篇)
1.面向对象高级语法部分 1.1 静态方法 通过 @staticmethod 装饰器即可把其装饰的方法变为一个静态方法,什么是静态方法呢?其实不难理解,普通的方法,可以在实例化后直接调用,并且在方法里 ...
- break prefab instance的原理
prefab可无限apply: 如果把一个模块做成了prefab,这个prefab可能在同一个scene中添加多个,甚至添加到了多个scene中.设所有这些实例为instance(1),instanc ...