沉睡森林@漂在北京

本处文章除注明“转载”外均为原创,转载请注明出处。

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  152 随笔 :: 4 文章 :: 114 评论 :: 0 Trackbacks
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 on 2010-03-14 17:46 王总兵 阅读(584) 评论(1)  编辑  收藏

评论

# re: Hibernate Annotation 中配置EhCache缓存 2011-08-03 23:09 we are people
不能发政治相关的内容------我要投票权!  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: