Java 使用 Spring 休眠二级缓存

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/474159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:05:52  来源:igfitidea点击:

Hibernate second level cache with Spring

javahibernatespringjpajakarta-ee

提问by Steve Kuo

I'm using Spring + JPA + Hibernate. I'm trying to enable Hibernate's second level cache. In my Spring's applicationContext.xmlI have:

我正在使用 Spring + JPA + Hibernate。我正在尝试启用 Hibernate 的二级缓存。在我的春天,applicationContext.xml我有:

<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:

所以它抱怨我没有启用二级缓存。我尝试通过添加到我的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:

但还是没有快乐。我还尝试将其添加到我的 ehcache.xml 中:

<property name="hibernate.cache.use_second_level_cache">true</property>

But it still doesn't work. Changing the provider_classto org.hibernate.cache.EhCacheProviderdoesn't help either:

但它仍然不起作用。更改provider_classorg.hibernate.cache.EhCacheProvider也无济于事:

<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下:

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">

Resolved:Since I'm using LocalEntityManagerFactoryBeanit gets its settings from META-INF/persistence.xml. My settings in applicationContext.xmlweren't even being read.

已解决:由于我正在使用LocalEntityManagerFactoryBean它从META-INF/persistence.xml. 我的设置applicationContext.xml甚至没有被读取。

采纳答案by Mainguy

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 LocalEntityManagerFactoryBeanit gets its settings from META-INF/persistence.xml. My settings in applicationContext.xmlweren't even being read.

由于我正在使用LocalEntityManagerFactoryBean它从META-INF/persistence.xml. 我的设置applicationContext.xml甚至没有被读取。

回答by Bitmap

Try this:

尝试这个:

<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.max_fetch_depth">4</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.EhCacheRegionFactory</prop>

And if you are using Maven add this to your POM file:

如果您使用 Maven,请将其添加到您的 POM 文件中:

<dependency>
  <groupId>net.sf.ehcache</groupId>
  <artifactId>ehcache-core</artifactId>
  <version>2.3.0</version>
</dependency>

Or download the latest jar from http://ehcache.org/

或者从http://ehcache.org/下载最新的 jar

回答by Deepak Kashyap

This linkhelped me for using second level cache with Hibernate 4

此链接帮助我在 Hibernate 4 中使用二级缓存