【问题标题】:javax.persistence.cache.retrieveMode and javax.persistence.cache.retrieveMode does not work with NamedQuery when used with Eclipse Link ORMjavax.persistence.cache.retrieveMode 和 javax.persistence.cache.retrieveMode 在与 Eclipse Link ORM 一起使用时不适用于 NamedQuery
【发布时间】:2017-07-01 06:26:30
【问题描述】:

当用于从二级缓存中获取数据时,查询提示在 Eclipse Link 2.3.2/2.6.1 中不起作用 使用提示,

@QueryHint(name = "javax.persistence.cache.retrieveMode", value = "USE"),
@QueryHint(name = "javax.persistence.cache.storeMode ", value = "USE")

尝试使用以下选项。

1. Added JPA Hints to Named query itself
@NamedQuery(
name = TestEntity.FIND_BY_CODE,
query = "select t from Test t where t.code = :code",
hints = {
@QueryHint(name = "javax.persistence.cache.retrieveMode", value = "USE"),
@QueryHint(name = "javax.persistence.cache.storeMode ", value = "USE") })

2. Adding hints to the Entity Manager Itself after injecting it
em.setProperty("javax.persistence.cache.retrieveMode", CacheRetrieveMode.USE);
em.setProperty("javax.persistence.cache.storeMode", CacheRetrieveMode.USE);

3. Added JPA hints at the time of Query execution
em.createNamedQuery(TestEntity.FIND_BY_CODE,
AlertCategoryType.class).setHint("javax.persistence.cache.retrieveMode", CacheRetrieveMode.USE)
.setHint("javax.persistence.cache.storeMode", CacheStoreMode.USE)
.setParameter("code", code).getSingleResult();

以上提示的使用均无效。然后我尝试调试我发现的三个不同选项, 设置这些提示后形成的数据库查询将提示作为下面的键/值对传递。

eclipselink.query.hints => {javax.persistence.cache.retrieveMode=USE,                                                                                                                      javax.persistence.cache.storeMode=USE} 

即使我们设置了 JPA 提示,eclipselink.query.hints 也是关键所在。这是我们无法控制的事情。
但是当我通过 Eclipse Link 提供的提示如下时,它开始按预期工作,结果是从缓存中获取的,而不是从数据库中获取的。

eclipselink.query.hints => {eclipselink.query-results-cache.size=500, eclipselink.query-results-cache=true} 

这意味着当我们使用 Eclipse Link 时,它只会根据我们在 Query 中看到的键[如上所示]识别 Eclipse Link 提供的提示。

请建议任何解决方法以使 JPA 提示正常工作

我使用的环境是

  • Eclispe Link 2.3.2/2.6.1
  • Runnin fin serve Glassfish 4.1[payara]
  • Java8/JEE7

【问题讨论】:

    标签: jpa eclipselink glassfish-4 second-level-cache


    【解决方案1】:

    您声明的查询提示(eclipselink.query-results-cache)完全不相关 - 它为查询结果创建一个新缓存,以便下次执行相同的查询时,结果已经存在,所以它不需要再次执行查询。这是在二级缓存之外(超出)。

    您提到的设置不工作会影响二级缓存。如果没有更多信息,我将声明它们可能按预期工作。仅仅因为您的查询进入数据库并不意味着没有使用缓存。缓存实体与将结果缓存到查询非常不同。如果查询结果没有被缓存,除非你启用了内存查询,否则大多数 read-all 类型的查询必须去数据库来确定需要构建和返回哪些实体。然后 EclipseLink 将使用这些结果来检查缓存 - 如果实体已经存在,它们将按原样返回 - 这避免了从数据重建实体的开销。

    您可以使用 em.find() 或使用 ID 值的读取查询来检查您的实体是否已被缓存。缓存是按 ID 索引的,所以它不需要去数据库找出你想要的实体。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-27
      • 2015-07-21
      • 2023-03-24
      相关资源
      最近更新 更多