}
执行结果:Hibernate: selectcustomers0_.id as id0_, customers0_.realName as realName0_, customers0_.pass aspass0_, customers0_.sex as sex0_, customers0_.petName as petName0_,customers0_.email as email0_, customers0_.rdate as rdate0_ from Customers customers0_
}
}
执行结果:Hibernate: select customers0_.id as col_0_0_ from Customers customers0_
Hibernate: selectcustomers0_.id as id0_0_, customers0_.realName as realName0_0_,customers0_.pass as pass0_0_, customers0_.sex as sex0_0_, customers0_.petNameas petName0_0_, customers0_.email as email0_0_, customers0_.rdate as rdate0_0_from Customers customers0_ where customers0_.id=?
null
Hibernate: selectcustomers0_.id as id0_0_, customers0_.realName as realName0_0_,customers0_.pass as pass0_0_, customers0_.sex as sex0_0_, customers0_.petNameas petName0_0_, customers0_.email as email0_0_, customers0_.rdate as rdate0_0_from Customers customers0_ where customers0_.id=?
asd
Hibernate: selectcustomers0_.id as id0_0_, customers0_.realName as realName0_0_,customers0_.pass as pass0_0_, customers0_.sex as sex0_0_, customers0_.petNameas petName0_0_, customers0_.email as email0_0_, customers0_.rdate as rdate0_0_from Customers customers0_ where customers0_.id=?
Null
很明显的看出:Iterator输入的sql语句多:N+1条语句
总结:
Query的两个方法,list()和 iterate() , 两个方法都是把结果集列出来, 他们有3点不一样,
1:返回的类型不一样,list()返回List,iterate()返回Iterator,
2: 获取数据的方式不一样,list()会直接查数据库,iterate()会先到数据库中把id都取出来,然后真正要遍历某个对象的时候先到缓存中找,如果找不到,以id为条件再发一条sql到数据库,这样如果缓存中没有数据,则查询数据库的次数为n+1。
3:iterate会查询2级缓存, list只会查询一级缓存。
4: list()中返回的List中每个对象都是原本的对象,iterate()中返回的对象是代理对象.(debug可以发现)
list()方法在执行时,直接运行查询结果所需要的查询语句。
iteraor()方法则是先执行得到对象ID的查询,然后在根据每个ID值去取得所要查询的对象。
因此:对于list()方式的查询通常只会执行一个SQL语句,而对于iterator()方法的查询则可能需要执行N+1条SQL语句(N为结果集中的记录数).
结果集的处理方法不同:
list()方法会一次取出所有的结果集对象,而且他会依据查询的结果初始化所有的结果集对象。如果在结果集非常庞大的时候会占据非常多的内存,甚至会造成内存溢出的情况发生。
iterator()方法在执行时不会一次初始化所有的对象,而是根据对结果集的访问情况来初始化对象。一次在访问中可以控制缓存中对象的数量,以避免占用过多的缓存,导致内存溢出情况的发生。