如何使用Hibernate 3.5.2配置JPA 2.0以将EHCache用作二级缓存和查询缓存? [英] How to configure JPA 2.0 with Hibernate 3.5.2 to use EHCache as a Level 2 cache and query cache?
问题描述
我找到了一些说明如何配置纯粹的hibernate使用EHCache。但我找不到任何说明如何配置JPA2.0 EntityManager使用缓存。 Hibernate 3.5.2是我的JPA2.0提供程序。
编辑//
@Cacheable(true)
足够实体吗?或者是否应该使用 @ org.hibernate.annotations.Cache
来配置实体?
我发现了一些说明如何配置纯粹的hibernate来使用EHCache。但我找不到任何说明如何配置JPA2.0 EntityManager使用缓存。 Hibernate 3.5.2是我的JPA2.0提供程序。
使用JPA配置L2缓存提供程序的方式类似于原始休眠。
默认情况下,Hibernate 3.5随附EhCache 1.5(请参阅配置Ehcache为二级缓存),如果你想使用Hibernate提供的官方缓存提供程序(在 hibernate-ehcache $ c $如果您使用的是Maven,请声明:
<! - 这是Hibernate提供的Ehcache提供程序,使用旧SPI - >
< property name =hibernate.cache.provider_classvalue =org.hibernate.cache.EhCacheProvider/>
如果你想使用EhCache 2.x,你需要使用EhCache提供的提供者它支持新的 Hibernate 3.3 / 3.5 SPI及其 CacheRegionFactory
)。使用:
<! - region factory属性是new属性(对于Hibernate 3.3及更高版本) - >
< property name =hibernate.cache.region.factory_classvalue =net.sf.ehcache.hibernate.EhCacheRegionFactory>
用于创建实例,或者
< property name =hibernate.cache.region.factory_classvalue =net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory/>
强制Hibernate使用Ehcache CacheManager的单例。
然后激活L2缓存和查询缓存:
< property name =hibernate.cache.use_second_level_cache值= 真/>
< property name =hibernate.cache.use_query_cachevalue =true/>
这是Hibernate L2缓存设置。
< blockquote>
@Cacheable(true)足够实体吗?或者我应该使用@ org.hibernate.annotations.Cache来配置实体?
理论上, @Cacheable
应该是Hibernate专有注释的替代品,应该与共享缓存模式
元素结合使用: p>
< persistence xmlns =http://java.sun.com/xml/ns/persistencexmlns:xsi =http ://www.w3.org/2001/XMLSchema-instancexsi:schemaLocation =http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence /persistence_2_0.xsdversion =2.0>
< persistence-unit name =FooPutransaction-type =RESOURCE_LOCAL>
< provider> org.hibernate.ejb.HibernatePersistence< / provider>
...
< shared-cache-mode> ENABLE_SELECTIVE< / shared-cache-mode>
<属性>
...
< / properties>
< / persistence-unit>
< /余辉>
但正如这个前面的问题,最初的实验并未成功(它可能与 HHH-5303 ,我不能说,我没有调查那么多)。所以我建议坚持专有注释。
引用
- Hibernate EntityManager参考指南
- JPA 2.0规范
- 第3.7.1节共享缓存元模型元素
- 第11.1.7节缓存注释
资源
相关问题
I found some instructions how to configure pure hibernate to use EHCache. But I can't find any instructions how to configure JPA2.0 EntityManager to use cache. Hibernate 3.5.2 is my JPA2.0 provider.
edit// Is
@Cacheable(true)
enough for entity? Or should I use@org.hibernate.annotations.Cache
to configure the entity?解决方案I found some instructions how to configure pure hibernate to use EHCache. But I can't find any instructions how to configure JPA2.0 EntityManager to use cache. Hibernate 3.5.2 is my JPA2.0 provider.
The way you configure the L2 cache provider with JPA is similar is similar to raw Hibernate.
By default, Hibernate 3.5 ships with EhCache 1.5 (see Configure Ehcache as a Second Level Cache) and if you want to use the official cache provider provided by Hibernate (in
hibernate-ehcache
if you are using Maven), declare:<!-- This is the provider for Ehcache provided by Hibernate, using the "old" SPI --> <property name="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider"/>
If you want to use EhCache 2.x, you'll need to use the provider provided by EhCache which supports the new Hibernate 3.3/3.5 SPI with its
CacheRegionFactory
). Use:<!-- 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">
for instance creation, or
<property name="hibernate.cache.region.factory_class" value="net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory"/>
to force Hibernate to use a singleton of Ehcache CacheManager.
And then activate L2 caching and query caching:
<property name="hibernate.cache.use_second_level_cache" value="true"/> <property name="hibernate.cache.use_query_cache" value="true"/>
That's for the Hibernate L2 cache setup.
Is @Cacheable(true) enough for entity? Or should I use @org.hibernate.annotations.Cache to configure the entity?
In theory, the
@Cacheable
is supposed to be a replacement for the Hibernate proprietary annotation and should be used in conjunction with theshared-cache-mode
element:<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>
But as mentioned in this previous question, initial experimentation has not been successful (it might be related to HHH-5303, I can't say, I didn't investigate that much). So I suggest sticking with the proprietary annotations.
References
- Hibernate EntityManager reference guide
- JPA 2.0 Specification
- Section 3.7.1 "The shared-cache-mode Element"
- Section 11.1.7 "Cacheable Annotation"
Resources
- Ehcache 2.0 supports new Hibernate 3.3 caching provider
- Configure Ehcache as a Second Level Cache
- Hibernate Second Level Cache
Related question
这篇关于如何使用Hibernate 3.5.2配置JPA 2.0以将EHCache用作二级缓存和查询缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!