我在Spring框架中使用Hibernate,并尝试缓存通过CriteriaAPI编写的查询。我已经启用了查询缓存和二级缓存。我还在该特定查询中将setCacheable设置为true。问题是,hibernate会话统计信息显示它会在数据库中找到我多次运行的同一查询的所有内容。
这里是标准查询的功能
@Override
public Collection<Article> findLatestArticle(int count) {
// TODO Auto-generated method stub
Criteria criteria = this.getSession()
.createCriteria(Article.class)
.setCacheable(true)
.setCacheRegion(CACHE_REGION)
.add(Restrictions.eq("isVisible", 1))
//.createCriteria("Count")
//.add(Restrictions.eq("Count", count))
.setFirstResult(0)
.setMaxResults(count);
return criteria.list();
}
这里是来电者功能
@Test
public void t1(){
List<Article> articleList = (List<Article>) articleDao.findLatestArticle(1);
//Article a = articleDao.findById(articlePk);
for(int i=0; i<19; i++)
articleDao.findLatestArticle(1);
Statistics stats = sessionFactory.getCurrentSession().getSessionFactory().getStatistics();
//System.out.println("2nd Lvl Stats: " + stats.getSecondLevelCacheStatistics(CACHE_REGION));
stats.logSummary();
System.out.println("Empty: " + articleList.isEmpty());
assertTrue(!articleList.isEmpty());
//assertNotNull(a);
}
spring-hibernate-context file
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
<prop key="hibernate.generate_statistics">true</prop>
<prop key="hibernate.cache.use_structured_entries">true</prop>
<prop key="net.sf.Ehcache.configurationResourceName">ehcache.xml</prop>
</props>
</property>
本课程的ehcache部分
<cache name="com.contento3.cms.article.model.Article"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="60"
overflowToDisk="true"/>
记录摘录:
2012-11-14 21:05:03,020 [main] INFO org.hibernate.cfg.SettingsFactory - Second-level cache: enabled
2012-11-14 21:05:03,020 [main] INFO org.hibernate.cfg.SettingsFactory - Query cache: enabled
2012-11-14 21:05:03,969 [main] INFO org.hibernate.cache.StandardQueryCache - starting query cache at region: com.contento3.cms.article.model.Article
2012-11-14 21:05:03,998 [main] INFO org.hibernate.stat.Statistics - HQL: from Article as a, time: 10ms, rows: 1
2012-11-14 21:05:04,010 [main] INFO org.hibernate.stat.Statistics - HQL: from Article as a, time: 1ms, rows: 1
2012-11-14 21:05:04,019 [main] INFO org.hibernate.stat.Statistics - HQL: from Article as a, time: 8ms, rows: 1
2012-11-14 21:05:04,022 [main] INFO org.hibernate.stat.Statistics - HQL: from Article as a, time: 2ms, rows: 1
2012-11-14 21:05:04,024 [main] INFO org.hibernate.stat.Statistics - HQL: from Article as a, time: 2ms, rows: 1
2012-11-14 21:05:04,026 [main] INFO org.hibernate.stat.Statistics - HQL: from Article as a, time: 2ms, rows: 1
2012-11-14 21:05:04,033 [main] INFO org.hibernate.stat.Statistics - HQL: from Article as a, time: 7ms, rows: 1
2012-11-14 21:05:04,035 [main] INFO org.hibernate.stat.Statistics - HQL: from Article as a, time: 2ms, rows: 1
2012-11-14 21:05:04,038 [main] INFO org.hibernate.stat.Statistics - HQL: from Article as a, time: 2ms, rows: 1
2012-11-14 21:05:04,041 [main] INFO org.hibernate.stat.Statistics - HQL: from Article as a, time: 2ms, rows: 1
2012-11-14 21:05:04,050 [main] INFO org.hibernate.stat.Statistics - HQL: from Article as a, time: 3ms, rows: 1
2012-11-14 21:05:04,059 [main] INFO org.hibernate.stat.Statistics - HQL: from Article as a, time: 9ms, rows: 1
2012-11-14 21:05:04,062 [main] INFO org.hibernate.stat.Statistics - HQL: from Article as a, time: 3ms, rows: 1
2012-11-14 21:05:04,067 [main] INFO org.hibernate.stat.Statistics - HQL: from Article as a, time: 4ms, rows: 1
2012-11-14 21:05:04,074 [main] INFO org.hibernate.stat.Statistics - HQL: from Article as a, time: 5ms, rows: 1
2012-11-14 21:05:04,077 [main] INFO org.hibernate.stat.Statistics - HQL: from Article as a, time: 2ms, rows: 1
2012-11-14 21:05:04,079 [main] INFO org.hibernate.stat.Statistics - HQL: from Article as a, time: 1ms, rows: 1
2012-11-14 21:05:04,081 [main] INFO org.hibernate.stat.Statistics - HQL: from Article as a, time: 1ms, rows: 1
2012-11-14 21:05:04,083 [main] INFO org.hibernate.stat.Statistics - HQL: from Article as a, time: 2ms, rows: 1
2012-11-14 21:05:04,085 [main] INFO org.hibernate.stat.Statistics - HQL: from Article as a, time: 2ms, rows: 1
2012-11-14 21:05:04,085 [main] INFO org.hibernate.stat.ConcurrentStatisticsImpl - Logging statistics....
2012-11-14 21:05:04,085 [main] INFO org.hibernate.stat.ConcurrentStatisticsImpl - start time: 1352909103053
2012-11-14 21:05:04,085 [main] INFO org.hibernate.stat.ConcurrentStatisticsImpl - sessions opened: 1
2012-11-14 21:05:04,085 [main] INFO org.hibernate.stat.ConcurrentStatisticsImpl - sessions closed: 0
2012-11-14 21:05:04,086 [main] INFO org.hibernate.stat.ConcurrentStatisticsImpl - transactions: 0
2012-11-14 21:05:04,086 [main] INFO org.hibernate.stat.ConcurrentStatisticsImpl - successful transactions: 0
2012-11-14 21:05:04,086 [main] INFO org.hibernate.stat.ConcurrentStatisticsImpl - optimistic lock failures: 0
2012-11-14 21:05:04,086 [main] INFO org.hibernate.stat.ConcurrentStatisticsImpl - flushes: 0
2012-11-14 21:05:04,086 [main] INFO org.hibernate.stat.ConcurrentStatisticsImpl - connections obtained: 1
2012-11-14 21:05:04,086 [main] INFO org.hibernate.stat.ConcurrentStatisticsImpl - statements prepared: 21
2012-11-14 21:05:04,086 [main] INFO org.hibernate.stat.ConcurrentStatisticsImpl - statements closed: 21
2012-11-14 21:05:04,086 [main] INFO org.hibernate.stat.ConcurrentStatisticsImpl - second level cache puts: 0
2012-11-14 21:05:04,086 [main] INFO org.hibernate.stat.ConcurrentStatisticsImpl - second level cache hits: 0
2012-11-14 21:05:04,086 [main] INFO org.hibernate.stat.ConcurrentStatisticsImpl - second level cache misses: 0
2012-11-14 21:05:04,086 [main] INFO org.hibernate.stat.ConcurrentStatisticsImpl - entities loaded: 0
2012-11-14 21:05:04,086 [main] INFO org.hibernate.stat.ConcurrentStatisticsImpl - entities updated: 0
2012-11-14 21:05:04,086 [main] INFO org.hibernate.stat.ConcurrentStatisticsImpl - entities inserted: 1
2012-11-14 21:05:04,086 [main] INFO org.hibernate.stat.ConcurrentStatisticsImpl - entities deleted: 0
2012-11-14 21:05:04,086 [main] INFO org.hibernate.stat.ConcurrentStatisticsImpl - entities fetched (minimize this): 0
2012-11-14 21:05:04,087 [main] INFO org.hibernate.stat.ConcurrentStatisticsImpl - collections loaded: 0
2012-11-14 21:05:04,087 [main] INFO org.hibernate.stat.ConcurrentStatisticsImpl - collections updated: 0
2012-11-14 21:05:04,087 [main] INFO org.hibernate.stat.ConcurrentStatisticsImpl - collections removed: 0
2012-11-14 21:05:04,087 [main] INFO org.hibernate.stat.ConcurrentStatisticsImpl - collections recreated: 0
2012-11-14 21:05:04,087 [main] INFO org.hibernate.stat.ConcurrentStatisticsImpl - collections fetched (minimize this): 0
2012-11-14 21:05:04,087 [main] INFO org.hibernate.stat.ConcurrentStatisticsImpl - queries executed to database: 20
2012-11-14 21:05:04,087 [main] INFO org.hibernate.stat.ConcurrentStatisticsImpl - query cache puts: 20
2012-11-14 21:05:04,087 [main] INFO org.hibernate.stat.ConcurrentStatisticsImpl - query cache hits: 0
2012-11-14 21:05:04,087 [main] INFO org.hibernate.stat.ConcurrentStatisticsImpl - query cache misses: 20
2012-11-14 21:05:04,087 [main] INFO org.hibernate.stat.ConcurrentStatisticsImpl - max query time: 10ms
我是Spring和Hibernate的新手。我已经阅读了至少十几篇有关此问题的文章,并尽我所能。另外,我使用createQuery方法编写了类似的查询,结果是一样的。任何帮助将不胜感激。
答案 0 :(得分:0)
尝试为此启用注释 @Cacheable @cache与缓存策略