Query.list() 和 Query.Iterate() 的区别

iterate()方法
Return the query results as an Iterator. If the query contains multiple results pre row, the results are returned in aninstance of Object[]. Entities returned as results are initialized ondemand. The first SQL query returns identifiers only.
(返回的实体只有在使用时才会被初始化,执行该方法查询时只会返回实体的标识符(即id))

list()方法
Return the query results as a List. If the query contains multiple results pre row, the results are returned in aninstance of Object[].

区别:

对于Query接口的list()方法与iterate()方法来说,都可以实现获取查询的对象,但是list()方法返回的每个对象都是完整的(对象中的每个属性都被表中的字段填充上了),而iterator()方法所返回的对象中仅包含了主键值(标识符),只有当你对iterator中的对象进行操作时,Hibernate才会向数据库再次发送SQL语句来获取该对象的属性值

list: 结果存入缓存,但不从缓存里面取;查询时属性连同id一起找出来,只有一句select;

iterate:结果存入缓存,并在缓存中查找结果;查询时先找出所有的 id,然后根据 id 到缓存里面查找,如果没有命中,再到数据库中查找该id对应的其他属性。可能会有多行 select。

猜你喜欢

转载自lstoryc.iteye.com/blog/1999275