java query.list 和 query.iterate 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14957220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
difference between query.list and query.iterate
提问by Avinash R
What exactly is the difference between using Query.list()
and Query.iterator()
?
usingQuery.list()
和之间究竟有什么区别Query.iterator()
?
Is there is any performance enhancement in using either. I mean that is any of them implementing lazy loading?
使用两者是否有任何性能增强。我的意思是他们中的任何一个都实现了延迟加载?
Or is Query.iterator()ultimately same as query.list().iterate()
或者Query.iterator()最终与query.list().iterate()
Also why is there no only Criteria.iterator()
Criteria.list()
还有为什么没有只有Criteria.iterator()
Criteria.list()
采纳答案by Suresh Atta
If instances are already in the session (primary-level cache) or second-level cache iterate()
will give better performance.
如果实例已经在会话中(一级缓存)或二级缓存iterate()
将提供更好的性能。
If they are not already cached, iterate()
will be slower than list()
and might require many database hits for a simple query.
如果它们尚未缓存,iterate()
则将比list()
简单查询慢并且可能需要多次数据库命中。
回答by Amitabha Roy
Query.list():Executes 1 SQL query and loads the entire data. Even if the records are present in cache a fresh SQL query is executed to load the records from the database.
Query.list():执行 1 个 SQL 查询并加载整个数据。即使记录存在于缓存中,也会执行新的 SQL 查询以从数据库加载记录。
List<Employee> list1 = session.createQuery("from Employee").list(); // SELECT *FROM EMP
for (Employee e : list1) {
System.out.println(e);
}
List<Employee> list2 = session.createQuery("from Employee").list(); // SELECT *FROM EMP
for (Employee e : list2) {
System.out.println(e);
}
Query.iterate():Executes 1+N SQL queries. The first query only returns the identifier of all the records and when the returned iterator is iterated then each time a separate SQL query is executed that contains a WHERE clause like “WHERE id=N”. If the records are present in cache then the first query is executed and the rest N queries are not executed and records are obtained from cache.
Query.iterate():执行 1+N 个 SQL 查询。第一个查询只返回所有记录的标识符,当返回的迭代器被迭代时,每次执行单独的 SQL 查询时都会包含一个 WHERE 子句,如“WHERE id=N”。如果记录存在于缓存中,则执行第一个查询,不执行其余 N 个查询,并从缓存中获取记录。
Iterator<Employee> iterator1 = session.createQuery("from Employee").iterate(); // SELECT EMP_ID FROM EMP
while(iterator1.hasNext()) {
System.out.println(iterator1.next()); // SELECT * FROM EMP WHERE EMP_ID=?
}
Iterator<Employee> iterator2 = session.createQuery("from Employee").iterate(); // SELECT EMP_ID FROM EMP
while (iterator2.hasNext()) {
System.out.println(iterator2.next()); // From cache, no SQL
}
回答by JB Nizet
The javadoc says:
javadoc 说:
Return the query results as an Iterator. If the query contains multiple results pre row, the results are returned in an instance of Object[].
Entities returned as results are initialized on demand. The first SQL query returns identifiers only.
将查询结果作为迭代器返回。如果查询包含多个结果 pre row,则结果在 Object[] 的实例中返回。
作为结果返回的实体按需初始化。第一个 SQL 查询仅返回标识符。
(emphasis mine)
(强调我的)
回答by Premraj
+----------------------------------------------+-----------------------------------------------+
| list() | iterate() |
+----------------------------------------------+-----------------------------------------------+
| Return type is List | Return type is Iterate |
| All records loads at single database request | For each record, one database hit is made |
| This is faster if cache is not available | This is very slower if cache is not available |
| Eager loading | Lazy loading |
+----------------------------------------------+-----------------------------------------------+
For list():
对于列表():
Query query = session.createQuery("from Employee");
List list = query.list(); // SELECT * FROM EMP
Iterator iterator = list.iterator();
while(iterator.hasNext()){
}
For iterate():
对于迭代():
Query query = session.createQuery("from Employee");
Iterator iterator = query.iterate(); // SELECT * FROM EMP
while(iterator.hasNext()){
// SELECT * FROM EMP WHERE EMP_ID=?
}