`
HelloSure
  • 浏览: 308484 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

Query的list()与iterator()的区别

阅读更多
Query上有list()与iterator()方法,两者的差别在于list()方法在读取数据时,并不会利用到快取,而是直接再向数据库查询,而iterator()则将读取到的数据写到快取,并于读取时再次利用。

来看看下面的程序:

Session session = sessionFactory.openSession();
       
Query query = session.createQuery("from User");
List users = query.list();
users = query.list();

session.close();


这个程序片段会使用两次SQL来查询数据库:

Hibernate: select user0_.id as id, user0_.name as name0_, user0_.age as age0_ from user user0_
Hibernate: select user0_.id as id, user0_.name as name0_, user0_.age as age0_ from user user0_


如果在Session关闭之前,要再将所有数据在取出,可以使用iterator()方法,例如:

Session session = sessionFactory.openSession();

Query query = session.createQuery("from User");
Iterator users = query.iterate();
users = query.iterate();

session.close();


这个程序片段会使用一次SQL向数据库查询,第二次则直接从快取中取得数据:

Hibernate: select user0_.id as col_0_0_ from user user0_


由于使用iterator()方法时会使用到Session level快取,所以在查询大量数据时,会耗用大量的内存,必要时可以使用Session的evict()或clear()方法来清除快取。


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/pcno1/archive/2009/01/07/3725586.aspx
分享到:
评论

相关推荐

    Hibernate中的query 分页.doc

    13. for (Iterator iter=userList.iterator(); iter.hasNext();) { 14. User user = (User)iter.next(); 15. System.out.println(user.getId()); 16. System.out.println(user.getName()); 17. } 18. session....

    Struts2实现分页

    <s:iterator value="list" status="st"> <td><s:property value="username"/> <td><s:property value="password"/> <td><s:property value="power"/> <td><s:property value="cdate"/> <td><a href="input?user....

    hibernate二级缓存

    Hibernate二级缓存 在一个数据库系统中,如果缓存设置的... hibernate.cache.use_query_cache必须配置,如果想缓存使用findall()、list()、Iterator()、createCriteria()、 createQuery()等方法获得的数据结果集。

    hibernate更新数据方法小结

    代码如下:Usertable user=null; Session session=HibernateSessionFactory.getSession();... List l=q.list(); Iterator ite=l.iterator(); if(ite.hasNext()){ user=(Usertable)ite.next(); } return us

    cpp-算法精粹

    Binary Search Tree Iterator Binary Tree Zigzag Level Order Traversal Recover Binary Search Tree Same Tree Symmetric Tree Balanced Binary Tree Flatten Binary Tree to Linked List Populating Next Right ...

    dangdang和smartstruts2.rar

    通用电子商务购物平台 --------目的----------- 1.将前期学习技术熟练应用 2.了解项目开发流程,培养开发能力(编码能力、查错排错能力、自学新技术... <s:iterator value="list" var="l">对应与action里的getLIst方法。

    red5连接池

    final List l = t.query("SELECT * FROM jt_employee", new RowMapper() { public Object mapRow(ResultSet rs, int rowNum) throws SQLException { MappedRow(rs.getInt(1), rs.getString(2)); } }); ...

    LeetCode最全代码

    * [Linked List](https://github.com/kamyu104/LeetCode#linked-list) * [Stack](https://github.com/kamyu104/LeetCode#stack) * [Queue](https://github.com/kamyu104/LeetCode#queue) * [Heap]...

    Python Cookbook, 2nd Edition

    Building a Dict from a List of Alternating Keys and Values Recipe 4.13. Extracting a Subset of a Dictionary Recipe 4.14. Inverting a Dictionary Recipe 4.15. Associating Multiple Values with ...

    Python Cookbook英文版

    8.9 Using dtuple for Flexible Access to Query Results 8.10 Pretty-Printing the Contents of Database Cursors 8.11 Establishing Database Connections Lazily 8.12 Accessing a JDBC Database from a ...

    BobBuilder_app

    Pages in a b+tree are usually implemented as a list or array of child pointers and so while finding and inserting a value is a O(log k) operation the process actually has to move children around in ...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    Table of Contents Header Files The #define Guard Header File Dependencies Inline Functions The -inl.h Files Function Parameter Ordering Names and Order of Includes Scoping Namespaces Nested Classes ...

Global site tag (gtag.js) - Google Analytics