我发现一些关于如何配置纯Hibernate使用EHCache的说明。但我找不到任何有关如何配置JPA2.0 EntityManager使用缓存的说明。我的JPA2.0提供程序是Hibernate 3.5.2。
编辑//
@Cacheable(true)
对实体来说足够吗?还是我应该使用@org.hibernate.annotations.Cache
来配置实体?
我发现一些关于如何配置纯Hibernate使用EHCache的说明。但我找不到任何有关如何配置JPA2.0 EntityManager使用缓存的说明。我的JPA2.0提供程序是Hibernate 3.5.2。
编辑//
@Cacheable(true)
对实体来说足够吗?还是我应该使用@org.hibernate.annotations.Cache
来配置实体?
hibernate-ehcache
),请声明:<!-- This is the provider for Ehcache provided by Hibernate, using the "old" SPI -->
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider"/>
CacheRegionFactory
的新 Hibernate 3.3 / 3.5 SPI。使用:<!-- The region factory property is the "new" property (for Hibernate 3.3 and above) -->
<property name="hibernate.cache.region.factory_class" value="net.sf.ehcache.hibernate.EhCacheRegionFactory">
<property name="hibernate.cache.region.factory_class" value="net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory"/>
<property name="hibernate.cache.use_second_level_cache" value="true"/>
<property name="hibernate.cache.use_query_cache" value="true"/>
这是关于Hibernate L2缓存设置的。
@Cacheable(true)
对实体来说足够了吗?还是应该使用@org.hibernate.annotations.Cache
来配置实体?
理论上,@Cacheable
应该是Hibernate专有注释的替代品,并应与shared-cache-mode
元素一起使用:
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0">
<persistence-unit name="FooPu" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
...
<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
<properties>
...
</properties>
</persistence-unit>
</persistence>
但正如这个之前的问题中提到的那样,最初的实验并不成功(可能与HHH-5303有关,我不能确定,我没有深入调查)。因此,我建议坚持使用专有注释。
在 persistence.xml 文件中,您可以指定以下属性:
<property name="hibernate.cache.region.factory_class"
value="org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory" />
并使其生效:
<property name="hibernate.cache.use_second_level_cache" value="true" />