java - EhCache + hibernate

标签 java spring hibernate ehcache second-level-cache

我有以下问题: 我有一个返回 35 个结果的查询,我想保留在二级缓存中:

public List<Product> getAllProducts() {
        Session session = this.sessionfactory.getCurrentSession();
        String queryString = "from com.ewave.upromotions.objects.Product product where product.active=:active";
        Query query = session.createQuery(queryString); 
        query.setBoolean("active", true);
        query.setCacheable(true);
        query.setCacheRegion("productCache");
        List<Product> products =query.list();
        return products;
    }

我的对象如下:

@Entity
@Table(name="products",schema="test11")
@Cacheable
@Cache( usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Product implements Serializable {

//all setters and getters ommited:

}

我的 ehcache.xml 文件在/src/目录中:

<ehcache>  
    <diskStore path="java.io.tmpdir"/>  
    <defaultCache maxElementsInMemory="10000"  
        eternal="false"  
        timeToIdleSeconds="120"  
        timeToLiveSeconds="120"  
        overflowToDisk="false"/>  
    <cache name="hibernate.test.org.hibernate.cache.UpdateTimestampsCache"   
        maxElementsInMemory="10000"  
        eternal="false"  
        timeToIdleSeconds="120"  
        timeToLiveSeconds="120"  
        overflowToDisk="true"/>  
    <cache name="hibernate.test.org.hibernate.cache.StandardQueryCache"   
        maxElementsInMemory="10000"  
        eternal="false"  
        timeToIdleSeconds="120"  
        timeToLiveSeconds="120"  
        overflowToDisk="true"/>
     <cache name="com.vanilla.objects.Product"
        maxElementsInMemory="300"
        eternal="false"
        overflowToDisk="false"
        timeToIdleSeconds="600"  
        timeToLiveSeconds="600"  
        />  
       <cache name="productCache" 
       maxElementsInMemory="100" 
       eternal="true" 
       overflowToDisk="false" />

</ehcache>  

我的 Hibernate 配置是:

<props>
    <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop> 
    <prop key="hibernate.cache.use_second_level_cache">true</prop> 
     <prop key="hibernate.show_sql">true</prop> 
  <prop key="hibernate.cache.use_query_cache">true</prop> 
  <prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.SingletonEhCacheProvider</prop>
  <prop key="hibernate.connection.release_mode">after_transaction</prop>
  <prop key="hibernate.cache.provider_configuration_file_resource_path">/ehcache.xml</prop>
  </props>

我的问题如下:第一次打开页面时,我看到了包含所有 35 个结果的选择:

当我刷新页面时,我看到的不是从缓存中取出 35 个对象,而是 35 个选择语句,它们按 ID 一个一个地查询对象。

怎么了?

最佳答案

当查询被缓存但对象没有缓存时,我遇到了这种情况。在您的示例代码中,我看到查询引用了 com.ewave.upmotions.objects.Product,但您为 com.vanilla.objects.Product 定义了缓存区域。而且您没有为 Product 类提供任何缓存区域。所以我建议您为 Product 显式指定缓存区域并在缓存配置中定义它。首先,我会尝试在同一区域进行查询和对象,只是为了看看它是否有效,然后尝试单独的配置。

关于java - EhCache + hibernate ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6723977/

相关文章:

java - Spring Security 5 - 我的自定义 UserDetailsS​​ervice 未被调用

java - 为什么 Hibernate 在更新对象之前先从数据库查询对象

java - Hibernate 5.1.x命名策略(向后兼容Hibernate 4.x)

java - 有没有一种快速方法可以在主代码继续之前关闭线程?

java - Linux 上的 Tesseract 使 Glassfish 崩溃

java - 为 Spring Boot 实现字节服务

javax.validation 验证值列表?

java - 如何在 hibernate 中将条件结果对象插入数据库

java - 我们是否应该避免显式使用 wait/notify/notifyAll

java - Sonarlint 未显示 SonarQube java squid 问题(记录)