Hibernate的缓存纪要


1、在maven中,pom.xml文件中添加如下依赖:
  <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>  hibernate-ehcache</artifactId>
    <version>4.2.5.Final</version>
</dependency>
会在工程中添加如下三个jar包:
  ehcache-core-2.4.3.jar
  hibernate-ehcache-4.2.5.Final.jar
  slf4j-api-1.6.1.jar

注意版本号要一致。
2、在Hibernate.cfg.xml中配置EHcache
<property name="cache.use_second_level_cache">true</property>
<property name="cache.region.factory_class">
org.hibernate.cache.ehcache.EhCacheRegionFactory
</property>
3、加载EHcache的配置文件
<ehcache>
    <diskStore path="./target/tmp"/>
    <defaultCache
        maxElementsInMemory="1000"
        eternal="false"
        timeToIdleSeconds="120"
        timeToLiveSeconds="120"
        overflowToDisk="true"
        />
    <cache name="sampleCache1"
        maxElementsInMemory="10000"
        eternal="true"
        timeToIdleSeconds="300"
        timeToLiveSeconds="600"
        overflowToDisk="true"
        />
</ehcache>
4、在映射文件配置哪个类和哪个集合需要放入二级缓存,配置缓存策略
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 


"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="cn.com.bochy.entity.Employee" table="employee">
 <cache usage="read-only" region="sampleCache1"/> 
<id name="e_id">
<generator class="sequence">
<param name="sequence">emp_seq</param>
</generator>
</id>
<property name="e_name"></property>
<set name="sets" table="pro_emp_tab" >
  <cache usage="read-only" region="sampleCache1"/> 
<key>
<column name="e_id"></column>
</key>
<many-to-many class="cn.com.bochy.entity.Project">
<column name="p_id"></column>
</many-to-many>
</set>
</class>
</hibernate-mapping>

5、配置查询缓存
  1、在hibernate.cfg.xml中
<property name="cache.use_query_cache">true</property>
  2、query.setCacheable(true);


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值