Java 在JPA/hibernate/Spring的二级ehcache中缓存JPQL查询

Java 在JPA/hibernate/Spring的二级ehcache中缓存JPQL查询,java,hibernate,caching,jpa,jpql,Java,Hibernate,Caching,Jpa,Jpql,我在persistence.xml中设置了二级缓存,并将需要缓存的每个实体配置如下: persistence.xml: <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode> <properties> <property name="hibernate.cache.region.factory_class" value="net.sf.ehcache.hibernate.EhCa

我在persistence.xml中设置了二级缓存,并将需要缓存的每个实体配置如下:

persistence.xml:

<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
    <properties>
      <property name="hibernate.cache.region.factory_class" value="net.sf.ehcache.hibernate.EhCacheRegionFactory"/>
      <property name="hibernate.cache.use_second_level_cache" value="true"/>
      <property name="hibernate.cache.use_query_cache" value="true"/>
 </properties>
如何让JPQL查询访问缓存?直接的hibernate查询似乎具有这种可设置缓存的特性,但在JPQL中没有类似的特性

谢谢你的帮助


Greg

您是否能够让任何查询与缓存、JPQL或Hibernate查询一起工作?我使用JPQL作为第一个要测试的查询。所以答案是否定的,因为这是我尝试的第一个问题。我可能应该按主键运行一个简单的查询,并在第一个测试查询加载三个相关实体时查看是否命中缓存。我的感觉是,我的ehcache.xml配置可能有问题。该配置通常非常标准,在我看来还行。安装缓存可能需要一点时间,我在尝试缓存查询时使用
namedquerys
运气更好。不总是一个选项,但当您可以使用'm'时,它会很好地工作。要检查的一件事是在您的jpa-context.xml中,您有这个吗
<defaultCache eternal="false" maxElementsInMemory="100000"
    overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0"
    timeToLiveSeconds="3600" memoryStoreEvictionPolicy="LRU"/>


   <cache
        name="org.hibernate.cache.StandardQueryCache"
        maxEntriesLocalHeap="10000"
        eternal="false"
        timeToLiveSeconds="3600"
        memoryStoreEvictionPolicy="LRU"/>