Spring使用Hibernate二级缓存 [英] Hibernate second level cache with Spring
问题描述
我使用Spring + JPA + Hibernate。我试图启用Hibernate的二级缓存。在我的Spring的 applicationContext.xml
中,我有:
< prop key = 在hibernate.cache.provider_class > net.sf.ehcache.hibernate.SingletonEhCacheProvider< /丙>
< prop key =hibernate.cache.provider_configuration_file_resource_path> /ehcache.xml< / prop>
当我运行时出现错误:
原因:org.hibernate.HibernateException:无法在org.hibernate.cache.CacheFactory.createCache(CacheFactory.java:64)
中实例化缓存实现
导致:org.hibernate.cache.NoCachingEnabledException:二级缓存未启用[hibernate.cache.use_second_level_cache | hibernate.cache.use_query_cache]
在org.hibernate.cache.NoCacheProvider.buildCache(NoCacheProvider.java:21)
所以这是抱怨我没有启用二级缓存。我试图通过添加到我的 applicationContext.xml
来启用它:
< prop key =hibernate.cache.use_second_level_cache> true< / prop>
但是仍然没有快乐。我也尝试将其添加到我的ehcache.xml中:
< property name =hibernate.cache.use_second_level_cache> true< ; /性>
但它仍然无效。将 provider_class
更改为 org.hibernate.cache.EhCacheProvider
也无济于事:
< prop key =hibernate.cache.provider_class> org.hibernate.cache.EhCacheProvider< / prop>
我的实体类被注释为使用缓存
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
,如何启用二级缓存?
编辑:这是在bean下面:
< bean id =entityManagerFactoryclass =org.springframework.orm.jpa.LocalEntityManagerFactoryBean>
已解决:由于我使用 LocalEntityManagerFactoryBean
它从 META-INF / persistence.xml
获取它的设置。我在 applicationContext.xml
中的设置甚至没有被读取。
我没有回答这个问题,但海报自己找到答案并不明显。我重新发布他的答案:
已解决
由于我使用 LocalEntityManagerFactoryBean
它从 META-INF / persistence.xml
获取它的设置。我在 applicationContext.xml
中的设置甚至没有被读取。
I'm using Spring + JPA + Hibernate. I'm trying to enable Hibernate's second level cache. In my Spring's applicationContext.xml
I have:
<prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.SingletonEhCacheProvider</prop>
<prop key="hibernate.cache.provider_configuration_file_resource_path">/ehcache.xml</prop>
When I run I get the error:
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)
So it's complain I don't have second level cache enabled. I attempt to enable it by adding to my applicationContext.xml
:
<prop key="hibernate.cache.use_second_level_cache">true</prop>
But still no joy. I also tried adding this to my ehcache.xml:
<property name="hibernate.cache.use_second_level_cache">true</property>
But it still doesn't work. Changing the provider_class
to org.hibernate.cache.EhCacheProvider
doesn't help either:
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
My entity classes are annotated to use caching
@Cache(usage=CacheConcurrencyStrategy.READ_ONLY)
So, how do I enable second level cache?
Edit: This is under the bean:
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
Resolved: Since I'm using LocalEntityManagerFactoryBean
it gets its settings from META-INF/persistence.xml
. My settings in applicationContext.xml
weren't even being read.
I didn't answer this, but it's not obvious that the poster found the answer himself. I'm reposting his answer:
Resolved
Since I'm using LocalEntityManagerFactoryBean
it gets its settings from META-INF/persistence.xml
. My settings in applicationContext.xml
weren't even being read.
这篇关于Spring使用Hibernate二级缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!