Hibernate4 + ehcache.

开篇先写结论:
Hibernate4 想使用 ehcache 时做二级缓存时,不使用 EHCache 提供的:[b]hibernate.cache.region.factory_class[/b]
请无视 EHcache 网站上的 document , 那是针对 Hibernate 3.X 的.
Hibernate 4.X 有自己对其他 Cache 框架的支持.


具体修改的地方:
1.Hibernate Properties 配置.

hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory


2. Maven pom 文件.
* 去掉 ehcache-core 依赖.
* 添加 cglib 依赖.
* 添加 hibernate-ehcache 依赖.
(这个会获取它所依赖的 ehcache 版本,同样有其他 cache 缓存框架的支持, oscach jbossCache等 )

3. your_ehcache.xml
e.g:

<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<defaultCache maxElementsInMemory="1000" eternal="false" timeToIdleSeconds="3600" timeToLiveSeconds="3600"
overflowToDisk="false">
</defaultCache>
<cache name="org.hibernate.cache.internal.StandardQueryCache" maxElementsInMemory="5" eternal="false" timeToLiveSeconds="120">
</cache>
<cache name="org.hibernate.cache.spi.UpdateTimestampsCache" maxElementsInMemory="5000" eternal="true">
</cache>
</ehcache>


4. Java Class
e.g.

@Entity
@Table(name = "t_user")
@Cacheable
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class User{...}


PS: 如果按照原来方式配置,可能会出现以下异常:

Caused by: java.lang.NoClassDefFoundError: org/hibernate/cache/TimestampsRegion
Caused by: java.lang.ClassNotFoundException: org.hibernate.cache.EntityRegion
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值