Hibernate二级缓存配置(2)
2016-12-13 16:57
489 查看
一、hibernate二级缓存对于集合的配置。
看一下测试代码
结果:
发送了三条sql语句,现在我们想,能不能实现对集合的缓存,即只发送两条sql语句。
集合缓存的配置:
注意: 还需要配置集合中的元素对应的持久化类也使用二级缓存! 否则将会多出 n 条 SQL 语句.
二、ehcache配置文件
二、查询缓存: 默认情况下, 设置的缓存对 HQL 及 QBC 查询时无效的, 但可以通过以下方式使其是有效的
I. 在 hibernate 配置文件中声明开启查询缓存
true
II. 调用 Query 或 Criteria 的 setCacheable(true) 方法
III. 查询缓存依赖于二级缓存
看一下测试代码
@Test public void test5(){ Department dept = (Department) session.get(Department.class, 1); System.out.println(dept.getDeptName()); System.out.println(dept.getEmployees().size()); tax.commit(); session.close(); session = sessionFactory.openSession(); tax = session.beginTransaction(); Department dept2 = (Department) session.get(Department.class, 80); System.out.println(dept.getDeptName()); System.out.println(dept.getEmployees().size()); }
结果:
Hibernate: select department0_.ID as ID1_0_0_, department0_.DEPTNAME as DEPTNAME2_0_0_ from DEPARTMENTS department0_ where department0_.ID=? part_A Hibernate: select employees0_.DEPT_ID as DEPT_ID3_0_1_, employees0_.ID as ID1_1_1_, employees0_.ID as ID1_1_0_, employees0_.NAME as NAME2_1_0_, employees0_.DEPT_ID as DEPT_ID3_1_0_ from EMPLOYEES employees0_ where employees0_.DEPT_ID=? 1 Hibernate: select department0_.ID as ID1_0_0_, department0_.DEPTNAME as DEPTNAME2_0_0_ from DEPARTMENTS department0_ where department0_.ID=? part_A 1
发送了三条sql语句,现在我们想,能不能实现对集合的缓存,即只发送两条sql语句。
集合缓存的配置:
<class-cache usage="read-write" class="com.atgui.hibernate.employee.Employee"/> <collection-cache usage="read-write" collection="com.atgui.hibernate.employee.Department.employees"/> <class-cache usage="read-write" class="com.atgui.hibernate.employee.Department"/>
注意: 还需要配置集合中的元素对应的持久化类也使用二级缓存! 否则将会多出 n 条 SQL 语句.
二、ehcache配置文件
<!-- 设置缓存的默认数据过期策略 --> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" /> <!-- 设定具体的命名缓存的数据过期策略。每个命名缓存代表一个缓存区域 缓存区域(region):一个具有名称的缓存块,可以给每一个缓存块设置不同的缓存策略。 如果没有设置任何的缓存区域,则所有被缓存的对象,都将使用默认的缓存策略。即:<defaultCache.../> Hibernate 在不同的缓存区域保存不同的类/集合。 对于类而言,区域的名称是类名。如:com.atguigu.domain.Customer 对于集合而言,区域的名称是类名加属性名。如com.atguigu.domain.Customer.orders --> <!-- name: 设置缓存的名字,它的取值为类的全限定名或类的集合的名字 maxElementsInMemory: 设置基于内存的缓存中可存放的对象最大数目 eternal: 设置对象是否为永久的, true表示永不过期, 此时将忽略timeToIdleSeconds 和 timeToLiveSeconds属性; 默认值是false timeToIdleSeconds:设置对象空闲最长时间,以秒为单位, 超过这个时间,对象过期。 当对象过期时,EHCache会把它从缓存中清除。如果此值为0,表示对象可以无限期地处于空闲状态。 timeToLiveSeconds:设置对象生存最长时间,超过这个时间,对象过期。 如果此值为0,表示对象可以无限期地存在于缓存中. 该属性值必须大于或等于 timeToIdleSeconds 属性值 overflowToDisk:设置基于内存的缓存中的对象数目达到上限后,是否把溢出的对象写到基于硬盘的缓存中 --> <cache name="com.atguigu.hibernate.entities.Employee" maxElementsInMemory="1" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="600" overflowToDisk="true" />
二、查询缓存: 默认情况下, 设置的缓存对 HQL 及 QBC 查询时无效的, 但可以通过以下方式使其是有效的
I. 在 hibernate 配置文件中声明开启查询缓存
true
II. 调用 Query 或 Criteria 的 setCacheable(true) 方法
III. 查询缓存依赖于二级缓存
相关文章推荐
- hibernate二级缓存配置
- Hibernate的二级缓存的配置
- hibernate的二级缓存的配置使用
- spring中配置hibernate二级缓存
- 为Spring集成的Hibernate配置二级缓存
- Spring视频学习(十三)配置Hibernate的二级缓存
- Hibernate使用ehcahe进行二级缓存的配置
- hibernate一级缓存,二级缓存,三级缓存,缓存算法及配置。
- hibernate二级缓存的配置
- Spring+Hibernate下配置Hibernate二级缓存EhCache
- Hibernate中二级缓存的配置和使用
- 为Spring集成的Hibernate配置二级缓存
- Hibernate3 二级缓存的使用配置细节
- 为Spring集成的Hibernate配置二级缓存
- hibernate二级缓存的配置
- hibernate二级缓存配置
- Hibernate中二级缓存的配置和使用
- 配置Hibernate二级缓存--echcache
- Hibernate配置Ehcache二级缓存
- hibernate一级缓存,二级缓存,三级缓存,缓存算法及配置。