相关疑难解决方法(0)

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

通常,我使用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 …
Run Code Online (Sandbox Code Playgroud)

orm caching hibernate jpa jpa-2.0

46
推荐指数
2
解决办法
5万
查看次数

我们如何在应用程序范围内存储数据以用于缓存目的?

我在应用程序范围内存储数据.我想在每小时后清除这些数据.实际上使用它作为缓存一小时.实现这个的最佳方法是什么?之前我们使用会话范围来存储这些数据,并且在会话到期后它过去会过期.由于此数据在整个应用程序中是唯一的,因此我们希望将其存储在应用程序范

java jsp caching web-applications

4
推荐指数
1
解决办法
5512
查看次数

标签 统计

caching ×2

hibernate ×1

java ×1

jpa ×1

jpa-2.0 ×1

jsp ×1

orm ×1

web-applications ×1