iterate()方法查询一级缓存吗?在我机器上测试,怎么不查询一级缓存呢? @Test
public void testIterate() {
Session s = sf.getCurrentSession();
s.beginTransaction();
Query q = s.createQuery("from Category");
Iterator<Category> it = q.iterate(); while (it.hasNext()) {
Category c = it.next();
System.out.println(c.getId());
}
it = q.iterate();
while (it.hasNext()) {
Category c = it.next();
System.out.println(c.getId());
}
s.getTransaction().commit();
}
结果
Hibernate:
select
category0_.id as col_0_0_
from
Category category0_
1
2
3
4
5
6
7
8
9
10
Hibernate:
select
category0_.id as col_0_0_
from
Category category0_
1
2
3
4
5
6
7
8
9
10
为什么不查询一级缓存呢?不是说list 和 iterate 方法有一个区别就是iterate查询缓存吗?
public void testIterate() {
Session s = sf.getCurrentSession();
s.beginTransaction();
Query q = s.createQuery("from Category");
Iterator<Category> it = q.iterate(); while (it.hasNext()) {
Category c = it.next();
System.out.println(c.getId());
}
it = q.iterate();
while (it.hasNext()) {
Category c = it.next();
System.out.println(c.getId());
}
s.getTransaction().commit();
}
结果
Hibernate:
select
category0_.id as col_0_0_
from
Category category0_
1
2
3
4
5
6
7
8
9
10
Hibernate:
select
category0_.id as col_0_0_
from
Category category0_
1
2
3
4
5
6
7
8
9
10
为什么不查询一级缓存呢?不是说list 和 iterate 方法有一个区别就是iterate查询缓存吗?
我只知道他们有个很大的区别,iterate第一次查询只会查询出ID,当需要实体时才发出类似的select * from
Category where id=?
API也无说缓存的问题
嗯。楼上哥们说的那个区别我知道。我看的是马士兵的视频。。 @Test
public void testCache3() {
Session s = sf.getCurrentSession();
s.beginTransaction();
Iterator<Category> it1 = s.createQuery("from Category").setCacheable(true).iterate();
while (it1.hasNext()) {
Category c = it1.next();
System.out.println(c.getId());
}
Iterator<Category> it2 = s.createQuery("from Category").setCacheable(true).iterate();
s.getTransaction().commit();
}
2级缓存他也不查
我刚才测试list方法查询2级缓存(中的查询缓存)而iterate方法不查2级缓存(中的查询缓存)。到底是,马老师说错了。还是我哪配置的不对啊?我测试出来的结论是,query接口的list和iterate方法都不查询一级缓存。list方法查询二级缓存(中的查询缓存)
1.查询出所有符合数据的ID,此步骤无法利用缓存,所以就会出现你看靠的多次select id from category;
2.当你真正访问实体属性时执行select * from Category where id=? 此步骤可以利用一级缓存,前提是这个实体已经加载到内存中,如果内存中不存在则需要发起查询,反之不需要....这就是所谓的利用缓存..
代码1public static List queryList(String hql) {
Session session = null;
Transaction tx = null;
List result=null;
try {
session = getCurrentSession();
tx = session.beginTransaction();
Query query=session.createQuery(hql);
result = query.list();
query.list();
Iterator iterator=query.iterate();
if(iterator.hasNext()){
((Event)iterator.next()).getTitle();
}
tx.commit();
} catch (Exception e) {
try {
tx.rollback();
} catch (HibernateException e1) {
logger.error("transaction rollback failed", e);
}
logger.error("data insert failed", e);
} finally {
closeCurrentSession();
}
return result;
}下面的代码2两个SQL都会执行
代码2public static List queryList(String hql) {
Session session = null;
Transaction tx = null;
List result=null;
try {
session = getCurrentSession();
tx = session.beginTransaction();
Query query=session.createQuery(hql);
/*result = query.list();
query.list();*/
Iterator iterator=query.iterate();
if(iterator.hasNext()){
((Event)iterator.next()).getTitle();
}
tx.commit();
} catch (Exception e) {
try {
tx.rollback();
} catch (HibernateException e1) {
logger.error("transaction rollback failed", e);
}
logger.error("data insert failed", e);
} finally {
closeCurrentSession();
}
return result;
}
2你都不用说了你的第一条就是说iterate不查询1级缓存。因为如果他查询,就不用每次都发。