Spring数据(JPARepository)和#x2B;Ehcache二级缓存

Spring数据(JPARepository)和#x2B;Ehcache二级缓存,spring,hibernate,caching,jpa,ehcache,Spring,Hibernate,Caching,Jpa,Ehcache,是否有人与Ehcache二级缓存一起使用Spring数据?我无法正确配置它 JPA地产: <prop key="hibernate.cache.provider_class">org.hibernate.cache.SingletonEhCacheProvider</prop> <prop key="hibernate.cache.use_second_level_cache">true</prop>

是否有人与Ehcache二级缓存一起使用Spring数据?我无法正确配置它

JPA地产:

<prop key="hibernate.cache.provider_class">org.hibernate.cache.SingletonEhCacheProvider</prop>
            <prop key="hibernate.cache.use_second_level_cache">true</prop>
            <prop key="hibernate.cache.use_query_cache">true</prop>
            <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
            <prop key="hibernate.generate_statistics">true</prop>
            <prop key="net.sf.ehcache.configurationResourceName">/ad_lobby/ehcache.xml</prop>

有什么想法吗?

假设您使用的是
SpringJUnit4ClassRunner
,这将使您的测试方法在单个事务中运行。这基本上意味着您使用的是单个会话,第二级缓存不会被命中,只有第一级缓存(会话本身)。我已经找到了一个解决方案,它在我的测试用例中工作,稍后将发布。嗯,也许您是对的,因为我已经更改了我的测试:请参见此处()但是为了让它工作,我必须在JpaRepository类中用@QueryHints({@QueryHint(name=“org.hibernate.cacheable”,value=“true”)}标记我的方法。虽然我不确定二级缓存或查询缓存的工作原理:|这将启用查询缓存,或将查询/结果标记为可缓存。是的,我只是认为启用查询将记住此查询的结果或全局记住实体
<persistence-unit name="hibernatePersistenceUnit" transaction-type="RESOURCE_LOCAL">
    <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
    <properties>
        <property name="hibernate.hbm2ddl.auto" value="update"/>
    </properties>
</persistence-unit>
@Test
public void testCache() {
def device = new Device()
deviceRepository.save(device)
for (int i = 0; i < 25; i++) {
    long now = new Date().getTime()
    deviceRepository.findById(device.id)
    long dif = new Date().getTime() - now
    println('Difference: ' + dif)
}
}
second level cache puts=1
second level cache hits=0
second level cache misses=0