• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
夜真寒
博客园    首页    新随笔    联系   管理    订阅  订阅

Hibernate Annotation 中配置EhCache缓存

1.  首先设置EhCache,建立配置文件ehcache.xml,默认的位置在class-path,可以放到你的src目录下:

xml version="1.0" encoding="UTF-8"?>  
<ehcache>
 <diskStore path="java.io.tmpdir"/>
  <defaultCache
   maxElementsInMemory="10000"
   eternal="false"

   overflowToDisk="true"

   timeToIdleSeconds="300"
   timeToLiveSeconds="180"
   diskPersistent="false"
   diskExpiryThreadIntervalSeconds= "120"/>
ehcache>


 

  2.  在Hibernate配置文件中设置:

<hibernate-configuration>

<session-factory>……<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property><property name="cache.use_second_level_cache">true</property>……</session-factory></hibernate-configuration>



        此外,可以把cache.use_second_level_cache设置为false关闭所有的hibernate二级缓存。但此属性对指定<cache>的类缺省为true。

 

   3.  为了使用二级缓存,需要在每一个Hibernate Entity上配置。

@Entity  
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Forest { ... }

------------------------------------------------------

@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
@JoinColumn(name="CUST_ID")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public SortedSet getTickets() {
return tickets;
}

----------------------------------------------------------------

@Cache(
CacheConcurrencyStrategy usage(); (1)
String region() default ""; (2)
String include() default "all"; (3)
)



(1) usage: 提供缓存对象的事务隔离机制,可选值有以下几种
(NONE, READ_ONLY, NONSTRICT_READ_WRITE, READ_WRITE, TRANSACTIONAL)

(2) region (optional): 指定缓存的区域,默认是类的全限定名。利用缓存区域,可以更精确的指定每个区域的缓存超前策略。如果指定了缓存区域前缀(在hibernate.cfg.xml中设置cache.region_prefix属性为一个字符串),则所有的缓存区域名前将加上这个前缀。
(3) include (optional): all to include all properties, non-lazy to only include non lazy properties (default all).

如果不是使用annotation的话,则是在Hbm文件中添加cache usage="read-only"

posted @ 2011-10-26 12:52  夜真寒  阅读(3392)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3