如何配置 JPA 2.0 和 Hibernate 3.5.2 以使用 EHCache 作为二级缓存和查询缓存?

发布于 2024-09-17 23:41:19 字数 230 浏览 7 评论 0原文

我找到了一些如何配置纯休眠以使用 EHCache 的说明。但我找不到任何有关如何配置 JPA2.0 EntityManager 以使用缓存的说明。 Hibernate 3.5.2 是我的 JPA2.0 提供程序。

编辑// @Cacheable(true) 对于实体​​来说足够了吗?或者我应该使用 @org.hibernate.annotations.Cache 来配置实体?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

山田美奈子 2024-09-24 23:41:19

我找到了一些如何配置纯休眠以使用 EHCache 的说明。但我找不到任何有关如何配置 JPA2.0 EntityManager 以使用缓存的说明。 Hibernate 3.5.2 是我的 JPA2.0 提供程序。

使用 JPA 配置 L2 缓存提供程序的方式与原始 Hibernate 类似。

默认情况下,Hibernate 3.5 附带 EhCache 1.5(请参阅将 Ehcache 配置为二级缓存)如果您想使用 Hibernate 提供的官方缓存提供程序(如果您使用 Maven,则在 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"/>

如果您想使用 EhCache 2.x,您需要使用 EhCache 提供的提供程序,该提供程序支持 Hibernate 3.3/3.5 SPI 及其CacheRegionFactory)。用途:

<!-- 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"/>

强制Hibernate使用Ehcache CacheManager的单例。

然后激活 L2 缓存和查询缓存:

<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,我不能说,我没有调查那么多)。所以我建议坚持使用专有注释。

参考资料

  • 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.

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 the shared-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

Related question

咆哮 2024-09-24 23:41:19

在 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" />

in persistence.xml you can specify this property:

<property name="hibernate.cache.region.factory_class"
       value="org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory" />

and to make it active:

<property name="hibernate.cache.use_second_level_cache" value="true" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文