如何使用 Hibernate 3.5.2 配置 JPA 2.0 以使用 EHCache 作为 2 级缓存和查询缓存? [英] How to configure JPA 2.0 with Hibernate 3.5.2 to use EHCache as a Level 2 cache and query cache?

查看:13
本文介绍了如何使用 Hibernate 3.5.2 配置 JPA 2.0 以使用 EHCache 作为 2 级缓存和查询缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

edit// Is @Cacheable(true) enough for entity? Or should I use @org.hibernate.annotations.Cache to configure the entity?

推荐答案

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

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.

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

The way you configure the L2 cache provider with JPA is similar is similar to raw Hibernate.

默认情况下,Hibernate 3.5 附带 EhCache 1.5(请参阅将 Ehcache 配置为二级缓存) 并且如果您想使用 Hibernate 提供的官方缓存提供程序(在 hibernate-ehcache 中,如果您使用的是 Maven),请声明:

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

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

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

例如创建,或

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

强制 Hibernate 使用 Ehcache CacheManager 的单例.

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

这是 Hibernate L2 缓存设置.

That's for the Hibernate L2 cache setup.

@Cacheable(true) 对实体足够了吗?还是应该使用@org.hibernate.annotations.Cache 来配置实体?

Is @Cacheable(true) enough for entity? Or should I use @org.hibernate.annotations.Cache to configure the entity?

理论上,@Cacheable 应该是 Hibernate 专有注解的替代品,并且应该与 shared-cache-mode 元素结合使用:

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>

但如这个上一个问题所述,初步实验并未成功(可能与HHH-5303,我不能说,我没有调查那么多).所以我建议坚持使用专有注释.

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.

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆