Hibernate 二级缓存举例(4.2 Final版)
来源:互联网 发布:网络教育公共课统考 编辑:程序博客网 时间:2024/06/15 18:12
Hibernate 4.2里面的缓存
1、 hibernate 里面的缓存分成一级缓存与二级缓存与查询缓存
- 一级缓存:Session级别的缓存
- 二级缓存:SessionFactory级别的缓存
- 查询缓存
2、 一级缓存
@Testpublic void testCache() {Session s = sf.getCurrentSession();s.beginTransaction();Category c = (Category)s.load(Category.class, 1);System.out.println(c.getName());Category c1 = (Category)s.load(Category.class, 1);System.out.println(c1.getName());s.getTransaction().commit();Session s2 = sf.getCurrentSession();s2.beginTransaction();Category c2 = (Category)s2.load(Category.class, 1);System.out.println(c2.getName());s2.getTransaction().commit();}
在不使用二级缓存的情况下,如果在一个Session里面对一个对象进行Load两次,那么只查询一次,如果在不同的Session里面各Load一次,会进行两次查询,也就是第二个Session不能访问第一个Session的缓存
3、 二级缓存(在Hibernate里面使用二级缓存)
(一) 导入相应的Jar包
- ehcache-core-2.4.3.jar
- hibernate-ehcache-4.2.0.Final.jar
- slf4j-api-1.6.1.jar
(二) 打开缓存设置,并以EhCache为例子进行实验
<property name="hibernate.cache.use_second_level_cache">true</property> <property name="cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
(三) 设置缓存对象,在Category类上面加上注解@Cache
@Cache(usage=CacheConcurrencyStrategy.READ_ONLY)public class Category {
(四) 对二级缓存进行测试,此时在两个不同的Session里面进行Load也只会查询一次
@Testpublic void testCache2() {Session s = sf.getCurrentSession();s.beginTransaction();Category c = (Category)s.load(Category.class, 1);System.out.println(c.getName());s.getTransaction().commit();Session s2 = sf.getCurrentSession();s2.beginTransaction();Category c2 = (Category)s2.load(Category.class, 1);System.out.println(c2.getName());s2.getTransaction().commit();}
4、 查询缓存
- 设置查询缓存,查询缓存是以二级缓存为基础<!-- 启用查询缓存 -->
<property name="hibernate.cache.use_query_cache">true</property> - 查询测试(必须使用setCacheable(true)方法)
@Testpublic void testQueryCache() {Session s = sf.getCurrentSession();s.beginTransaction();List<Category> cs = (List<Category>)s.createQuery("from Category").setCacheable(true).list();for(Category c : cs) {System.out.println(c.getName());}List<Category> cs2 = (List<Category>)s.createQuery("from Category").setCacheable(true).list();for(Category c : cs2) {System.out.println(c.getName());}s.getTransaction().commit();Session s2 = sf.getCurrentSession();s2.beginTransaction();List<Category> cs3 = (List<Category>)s2.createQuery("from Category").setCacheable(true).list();for(Category c : cs3) {System.out.println(c.getName());}s2.getTransaction().commit();}
- Hibernate 二级缓存举例(4.2 Final版)
- Hibernate二级缓存(sessionFactory)
- Hibernate二级缓存(一)
- Hibernate (八)二级缓存
- Hibernate (十一) 二级缓存
- Hibernate(七)---二级缓存
- 使用hibernate-release-5.2.10.Final开启二级缓存配置
- 【Hibernate】二级缓存(Hibernate默认使用的二级缓存)
- hibernate一二级缓存(一)
- hibernate一二级缓存(二)
- EHCache(hibernate二级缓存插件)
- 二级缓存配置(spring+hibernate)
- Hibernate二级缓存配置(2)
- hibernate的二级缓存(一)
- Hibernate入门八(二级缓存)
- 【Hibernate】hibernate的二级缓存(spring)
- Hibernate总结(六)--Hibernate二级缓存
- hibernate 二级缓存
- 顺序表的实现
- 链表的实现
- 双向链表的实现
- 顺序栈的实现
- Hibernate 里面的1+N问题
- Hibernate 二级缓存举例(4.2 Final版)
- 链栈的实现
- sql server解锁及查看连接数
- 链式队列的实现
- 排序方法
- Windows 环境下文件批量操作
- 循环队列的实现
- 字符串功能实现
- 使用Jdom进行简单的XML解析