如何使用JPA2的@Cacheable代替Hibernate的@Cache

sma*_*ufo 46 orm caching hibernate jpa jpa-2.0

通常,我使用Hibernate的@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)来缓存@Entity类,它运行良好.

在JPA2中,还有另一个@Cacheable注释,它似乎与Hibernate的@Cache具有相同的功能.为了使我的实体类独立于hibernate的包,我想尝试一下.但我不能让它发挥作用.每次简单的id查询仍然会访问数据库.

谁能告诉我哪里出错了?谢谢.

实体类:

@Entity
//@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Cacheable(true) 
public class User implements Serializable
{
 // properties
}
Run Code Online (Sandbox Code Playgroud)

测试类:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:app.xml"})
@TransactionConfiguration(transactionManager="transactionManager")
public class UserCacheTest
{
  @Inject protected UserDao userDao;

  @Transactional
  @Test
  public void testGet1()
  {
    assertNotNull(userDao.get(2L));
  }

  @Transactional
  @Test
  public void testGet2()
  {
    assertNotNull(userDao.get(2L));
  }

  @Transactional
  @Test
  public void testGet3()
  {
    assertNotNull(userDao.get(2L));
  }
}
Run Code Online (Sandbox Code Playgroud)

测试结果显示每个"get"命中DB层(使用hibernate.show_sql = true).

Persistence.xml:

<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.use_outer_join" value="true"/>

<property name="hibernate.cache.provider_class" value="org.hibernate.cache.SingletonEhCacheProvider"/>
<property name="hibernate.cache.use_second_level_cache" value="true"/>
<property name="hibernate.cache.use_query_cache" value="true"/>
Run Code Online (Sandbox Code Playgroud)

JPA代码:

@Override
public T get(Serializable id)
{
  return em.find(clazz, id);
}
Run Code Online (Sandbox Code Playgroud)

Pas*_*ent 44

根据JPA 2.0规范,如果要使用@Cacheable注释选择性地缓存实体,则应该<shared-cache-mode>persistence.xml(或javax.persistence.sharedCache.mode 创建时的等效项EntityManagerFactory)中指定a .

下面是persistence.xml具有相关元素和属性的示例:

<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>
      ...
      <property name="hibernate.cache.provider_class" value="org.hibernate.cache.SingletonEhCacheProvider"/>
      <property name="hibernate.cache.use_second_level_cache" value="true"/>
      <property name="hibernate.cache.use_query_cache" value="true"/>
    </properties>
  </persistence-unit>
</persistence>
Run Code Online (Sandbox Code Playgroud)

请注意,我已经看到至少有一个问题HHH-5303与缓存有关.所以以上不保证:)

参考

  • Hibernate EntityManager参考指南
  • JPA 2.0规范
    • 第3.7.1节"共享缓存模式元素"
    • 第11.1.7节"可缓存的注释"


Joh*_*n29 11

对于那些使用Spring配置的人而言persistence.xml,这里有一个示例:

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="database" value="MYSQL"/>
            <property name="databasePlatform" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
            <property name="showSql" value="true"/>
            <property name="generateDdl" value="false"/>
        </bean>
    </property>
    <property name="packagesToScan" value="com.mycompany.myproject.domain"/>
    <property name="jpaPropertyMap">
        <map>
            <entry key="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory"/>
            <entry key="hibernate.cache.use_second_level_cache" value="true"/>
            <entry key="hibernate.cache.use_query_cache" value="true"/>
            <entry key="javax.persistence.sharedCache.mode" value="ENABLE_SELECTIVE" />
        </map>
    </property>
</bean>
Run Code Online (Sandbox Code Playgroud)

另请注意,如果您正在使用@Cacheable注释,则只能使用默认缓存并发策略,该策略由该getDefaultAccessType()方法确定RegionFactory.在EhCache的情况下,它是READ_WRITE.如果要使用其他策略,则必须使用Hibernate的@Cache注释.