Java 使用Spring休眠二级缓存
我正在使用Spring+JPA+Hibernate。我正在尝试启用Hibernate的二级缓存。在Spring的Java 使用Spring休眠二级缓存,java,hibernate,spring,jpa,jakarta-ee,Java,Hibernate,Spring,Jpa,Jakarta Ee,我正在使用Spring+JPA+Hibernate。我正在尝试启用Hibernate的二级缓存。在Spring的applicationContext.xml中,我有: <prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.SingletonEhCacheProvider</prop> <prop key="hibernate.cache.provider_configuration_f
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>
因此,我没有启用二级缓存是很遗憾的。我试图通过向我的applicationContext.xml
中添加以下内容来启用它:
<prop key="hibernate.cache.use_second_level_cache">true</prop>
我的实体类被注释为使用缓存
@Cache(usage=CacheConcurrencyStrategy.READ_ONLY)
那么,如何启用二级缓存
编辑:这在bean下:
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
已解决:因为我使用的是
LocalEntityManagerFactoryBean
它从META-INF/persistence.xml
获取设置。我在applicationContext.xml
中的设置甚至没有被读取。我没有回答这个问题,但不明显海报自己找到了答案。我转载了他的答案:
断然的
由于我使用的是LocalEntityManagerFactoryBean
,它从META-INF/persistence.xml
获取设置。我在applicationContext.xml中的设置甚至没有被读取。试试以下方法:
<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>
true
4.
真的
真的
net.sf.ehcache.hibernate.EhCacheRegionFactory
如果您正在使用Maven,请将其添加到POM文件中:
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.3.0</version>
</dependency>
net.sf.ehcache
帮助我在Hibernate 4中使用二级缓存你在哪个bean中设置这些道具?entityManagerFactory,它与我设置Hibernate的bean相同。方言,哪个Hibernate读取我不知道JPA,我们在org.springframework.orm.hibernate3.LocalSessionFactoryBeanYeah的hibernateProperties下有它,我试着把它放在hibernateProperties下,它也有同样的问题。它基本上不读取“hibernate.cache.provider_class”属性。我在创建LocalEntityManagerFactoryBean时将其设置为JPAPProperties。不过,我还是犯了同样的错误。任何建议??出于缓存的目的,max_fetch_depth选项是不必要的,将其设置为“4”可能是不可取的,因为它允许在SQL查询上发生大量连接。在大型数据库上,查询可以返回数百万个结果,执行和简化为可用实体需要花费很长时间。默认情况下,最大提取深度为“2”。
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<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>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.3.0</version>
</dependency>