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).
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"
本文转自博客园沉睡森林@漂在北京的博客,原文链接:Hibernate Annotation 中配置EhCache缓存,如需转载请自行联系原博主。