使用Spring进行Hibernate二级缓存
我正在使用Spring + JPA + Hibernate。 我正在尝试启用Hibernate的二级缓存。 在我的Spring的applicationContext.xml
我有:
net.sf.ehcache.hibernate.SingletonEhCacheProvider /ehcache.xml
当我运行时,我得到错误:
Caused by: org.hibernate.HibernateException: Could not instantiate cache implementation at org.hibernate.cache.CacheFactory.createCache(CacheFactory.java:64) Caused by: org.hibernate.cache.NoCachingEnabledException: Second-level cache is not enabled for usage [hibernate.cache.use_second_level_cache | hibernate.cache.use_query_cache] at org.hibernate.cache.NoCacheProvider.buildCache(NoCacheProvider.java:21)
所以它抱怨我没有启用二级缓存。 我尝试通过添加到我的applicationContext.xml
来启用它:
true
但仍然没有快乐。 我也尝试将其添加到我的ehcache.xml中:
true
但它仍然无效。 将provider_class
更改为org.hibernate.cache.EhCacheProvider
也无济于事:
org.hibernate.cache.EhCacheProvider
我的实体类被注释为使用缓存
@Cache(usage=CacheConcurrencyStrategy.READ_ONLY)
那么,我如何启用二级缓存?
编辑:这是在bean下:
已解决:由于我使用的是LocalEntityManagerFactoryBean
因此它从META-INF/persistence.xml
获取其设置。 我甚至没有读取applicationContext.xml
中的设置。
我没有回答这个问题,但海报发现答案并不明显。 我转发他的回答:
解决
由于我使用的是LocalEntityManagerFactoryBean
因此它从META-INF/persistence.xml
获取其设置。 我甚至没有读取applicationContext.xml
中的设置。
尝试这个:
true 4 true true net.sf.ehcache.hibernate.EhCacheRegionFactory
如果您使用Maven,请将其添加到您的POM文件中:
net.sf.ehcache ehcache-core 2.3.0
或者从http://ehcache.org/下载最新的jar
这个链接帮助我使用Hibernate 4的二级缓存