JPA本机查询二级缓存
我需要使用JPA为本机sql使用二级缓存。JPA本机查询二级缓存,jpa,Jpa,我需要使用JPA为本机sql使用二级缓存。 我没有找到做这件事的方法。如果JPA支持本机查询缓存,是否有人建议JPA使用@Cacheable注释支持二级缓存,请参阅官方文档。第二级缓存的配置是特定于持久性提供程序的,这是一个如何设置hibernate的示例 首先添加与缓存提供程序的集成,在本例中为ehcache: <dependency> <groupId>org.hibernate</groupId> <artifactId>hi
我没有找到做这件事的方法。如果JPA支持本机查询缓存,是否有人建议JPA使用
@Cacheable
注释支持二级缓存,请参阅官方文档。第二级缓存的配置是特定于持久性提供程序的,这是一个如何设置hibernate的示例
首先添加与缓存提供程序的集成,在本例中为ehcache:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>SOME-HIBERNATE-VERSION</version>
</dependency>
下面是如何缓存条件查询:
List cats = session.createCriteria(Cat.class)
.setCacheable(true)
.list();
JPA二级缓存是为实体设计的。如果没有,那么首先,使用JPA是没有意义的,其次,即使您有实体,本机查询也不一定返回它们,因此没有任何东西是可缓存的
显然,您可能会发现一个实现,它允许在二级缓存中缓存来自本机(SQL)查询的大量信息,但是您没有使用JPA规范标准,而是使用供应商规范(如果您更改了JPA提供程序,这将停止工作)我已经发布了一些ehcache缓存提供程序的配置示例,我写了一篇blg文章,介绍了二级缓存的缺陷,这可能也会有所帮助,看看吧,谢谢你的回复。我没有实体,也不想创建实体。我们已经完成了本机查询,所以现在我们想为其添加二级缓存。
<?xml version="1.0" ?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
updateCheck="false"
xsi:noNamespaceSchemaLocation="ehcache.xsd" name="yourCacheManager">
<diskStore path="java.io.tmpdir"/>
<cache name="yourEntityCache"
maxEntriesLocalHeap="10000"
eternal="false"
overflowToDisk="false"
timeToLiveSeconds="86400" />
<cache name="org.hibernate.cache.internal.StandardQueryCache"
maxElementsInMemory="10000"
eternal="false
timeToLiveSeconds="86400"
overflowToDisk="false"
memoryStoreEvictionPolicy="LRU" />
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToLiveSeconds="86400"
overflowToDisk="false"
memoryStoreEvictionPolicy="LRU" />
</ehcache>
@NamedQuery(name="account.queryName",
query="select acct from Account ...",
hints={
@QueryHint(name="org.hibernate.cacheable",
value="true")
}
})
List cats = session.createCriteria(Cat.class)
.setCacheable(true)
.list();