日期:2014-05-17  浏览次数:20616 次

hibernate 查询缓存问题
请问 我在 hibernate.cfg.xml 配置了 ,我使用的是 hibernate 3.6
<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<property name="hibernate.cache.use_query_cache">true</property>

配置了 ehcache.xml 直接在hibernate里面找的 复制过来 使用默认
<defaultCache
        maxElementsInMemory="10000"
        eternal="false"
        timeToIdleSeconds="120"
        timeToLiveSeconds="1200"
        overflowToDisk="true"
/>

我在用测试方法中 :
@Test
public void testQueryCache() {
Session session = new Configuration().configure().buildSessionFactory()
.getCurrentSession();
session.beginTransaction();
List<Category> categories = (List<Category>) session.createQuery(
"from Category").setCacheable(true).list();
/*
 * List<Category> categories2 = (List<Category>) session.createQuery(
 * "from Category").setCacheable(true).list();
 */
session.getTransaction().commit();

Session session2 = new Configuration().configure()
.buildSessionFactory().openSession();
session2.beginTransaction();
List<Category> categories2 = (List<Category>) session2.createQuery(
"from Category").setCacheable(true).list();
session2.getTransaction().commit();

}

为什么我跨session了 还是发2条select语句 不是应该1条吗 在同一session就是一条了
怎么才能实现跨session 只是发1条select 语句呢 谢谢 


------解决方案--------------------
开启二级缓存