Hibernate 在缓存中找不到查询结果

Hibernate 在缓存中找不到查询结果,hibernate,orm,jpa,caching,ehcache,Hibernate,Orm,Jpa,Caching,Ehcache,Env:jboss5.1、ehcache 2.1.0、hibernate 3.3.x、seam 2.2.0 xml(2.1.0版本)包含以下行,但在缓存中找不到我的查询结果。我应该为触发的每个查询设置一个缓存区域吗。我错过了什么 <!-- Cache configuration --> <cache name="org.hibernate.cache.UpdateTimestampsCache" maxElementsInMemory="5000" eternal="t

Env:jboss5.1、ehcache 2.1.0、hibernate 3.3.x、seam 2.2.0

xml(2.1.0版本)包含以下行,但在缓存中找不到我的查询结果。我应该为触发的每个查询设置一个缓存区域吗。我错过了什么

<!-- Cache configuration -->
<cache name="org.hibernate.cache.UpdateTimestampsCache"
    maxElementsInMemory="5000" eternal="true" timeToIdleSeconds="300"
    timeToLiveSeconds="300" overflowToDisk="true" diskPersistent="false"
    diskExpiryThreadIntervalSeconds="300" memoryStoreEvictionPolicy="LRU" />

<cache name="org.hibernate.cache.StandardQueryCache"
    maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="300"
    timeToLiveSeconds="300" overflowToDisk="true" diskPersistent="false"
    diskExpiryThreadIntervalSeconds="300" memoryStoreEvictionPolicy="LRU" />

我应该为触发的每个查询设置一个缓存区域吗。我错过了什么

<!-- Cache configuration -->
<cache name="org.hibernate.cache.UpdateTimestampsCache"
    maxElementsInMemory="5000" eternal="true" timeToIdleSeconds="300"
    timeToLiveSeconds="300" overflowToDisk="true" diskPersistent="false"
    diskExpiryThreadIntervalSeconds="300" memoryStoreEvictionPolicy="LRU" />

<cache name="org.hibernate.cache.StandardQueryCache"
    maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="300"
    timeToLiveSeconds="300" overflowToDisk="true" diskPersistent="false"
    diskExpiryThreadIntervalSeconds="300" memoryStoreEvictionPolicy="LRU" />
不,您不是(除非您希望对它们进行细粒度控制)。以下是有关该主题的文档说明:

查询结果集也可以缓存。 这仅对以下查询有用: 经常使用相同的 参数。你首先需要 启用查询缓存:

hibernate.cache.use_query_cache true
此设置将创建两个新缓存 区域:一个保存缓存查询的区域 结果集 (
org.hibernate.cache.StandardQueryCache
), 另一个保存 queryable的最新更新 桌子 (
org.hibernate.cache.UpdateTimestampsCache
)。 请注意,查询缓存没有 缓存实际实体的状态 在结果集中;它只能缓存 标识符值和值的结果 类型。查询缓存应始终保持不变 与 二级缓存

大多数查询不会从中受益 缓存,因此默认情况下,查询是 没有缓存。要启用缓存,请调用
Query.setCacheable(true)
。这个电话 允许查询查找现有的 缓存结果或将其结果添加到 执行缓存时,缓存将被删除

如果您需要细粒度的控制 过查询缓存过期策略, 可以指定命名缓存区域 通过调用
Query.setCacheRegion()

如果查询应强制刷新 它的查询缓存区域,您应该 呼叫
Query.setCacheMode(CacheMode.REFRESH)
。 这在某些情况下特别有用 基础数据可能已被删除的位置 通过单独流程更新(即。, 未通过Hibernate进行修改)和 允许应用程序有选择地 刷新特定查询结果集。 这是一个更有效的选择 退出查询缓存区域的步骤 通过
SessionFactory.executeQueries()

现在的问题是:

  • 是否启用了二级缓存
  • 您是否启用了查询所涉及的实体的缓存
  • 您是否通过调用
    setCacheable(true)
    来启用查询缓存
这是不相关的,但我也建议激活
org.hibernate.cache
类别的日志记录

另见

我已启用二级缓存@缓存注释已添加到实体类@Cache(usage=cacheconcurrencysttrategy.READ\u WRITE)中。我们使用EntityQuery框架,因此我们将可缓存标志设置为true的setHints()方法我已启用日志记录,因此我发现查询结果不可用cached@Samuel:这是一种“细节”这在你的问题中值得一提。这可能有助于获得更好的答案(读者也不会浪费时间)。我同意,作为问题本身的一部分,我应该这样做