快速业务通道

hibernate3学习笔记(二十二)|Query缓存机制 - 编程入门网

作者 佚名技术 来源 NET编程 浏览 发布时间 2012-06-19
_22.model.User;14. 15.public class BusinessService {16. 17. public static void main(String[] args) throws IOException, SQLException {18.19. Configuration config = new Configuration().configure();20. SessionFactory sessionFactory = config.buildSessionFactory(); 21. Session session = sessionFactory.openSession();22. 23.24. String hql = "from com.hb3.pack_22.model.User";25.26. Query query = session.createQuery(hql);27.28.// List<?> list = query.list(); 29.// Iterator<?> iterator = list.iterator(); 30.// while (iterator.hasNext()) { 31.// User user = (User)iterator.next(); 32.// System.out.println(user.getRoom().getAddress()); 33.// } 34.// System.out.println("====================================="); 35.// list = query.list(); 36.// iterator = list.iterator(); 37.// while (iterator.hasNext()) { 38.// User user = (User)iterator.next(); 39.// System.out.println(user.getRoom().getAddress()); 40.// } 41.//42.// System.out.println("++++++++++++++++++++++++++++++++++++++"); 43.44. query = session.createQuery(hql);45. Iterator<?> iterator = query.iterate();46. while (iterator.hasNext()) {47. User user = (User)iterator.next();48. System.out.println(user.getName());49. }50. System.out.println("=====================================");51. iterator = query.iterate();52. while (iterator.hasNext()) {53. User user = (User)iterator.next();54. System.out.println(user.getName());55. }56.57. session.close();58. sessionFactory.close();59. }60.}

hibernate3学习笔记(二十二)|Query缓存机制(5)

时间:2011-02-03

执行结果:

Hibernate: select user0_.id as col_0_0_ from user user0_ Hibernate: select user0_.id as id0_1_, user0_.name as name0_1_, user0_.room_id as room3_0_1_, room1_.id as id1_0_, room1_.address as address1_0_ from user user0_ left outer join room room1_ on user0_.room_id=room1_.id where user0_.id=? chenyan Hibernate: select user0_.id as id0_1_, user0_.name as name0_1_, user0_.room_id as room3_0_1_, room1_.id as id1_0_, room1_.address as address1_0_ from user user0_ left outer join room room1_ on user0_.room_id=room1_.id where user0_.id=? shenbin ===================================== Hibernate: select user0_.id as col_0_0_ from user user0_ chenyan shenbin

看见,Iterator中的元素并不是一次性返回的,而是一条一条返回的。当数据量庞大时,特性就会下降。

再看下面的测试代码:

1.package com.hb3.pack_22;2.3.import java.io.IOException;4.import java.sql.SQLException;5.import java.util.Iterator;6.import java.util.List;7.
8.import org.hibernate.Query;9.import org.hibernate.Session;10.import org.hibernate.SessionFactory;11.import org.hibernate.cfg.Configuration;12.
13.import com.hb3.pack_22.model.User;14.
15.public class BusinessService {16.
17.    pub

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号