hibernate iterate list 区别

iterate:

	        String sql = "from Bigque where id <= 5";
		String sql2 = "from Bigque where id <= 6";
		BigqueDAO dao = new BigqueDAO();
		Session session = dao.getSession();
		Iterator<Bigque> it = session.createQuery(sql).iterate();
		
		while(it.hasNext()){
			System.out.println(it.next().getContent());
		}
		
		System.out.println("===========");
		
                Iterator<Bigque> it2 = session.createQuery(sql2).iterate();
		
		while(it2.hasNext()){
			System.out.println(it2.next().getContent());
		}
		session.close();

结果:

Hibernate: select bigque0_.ID as col_0_0_ from cloudroom.bigque bigque0_ where bigque0_.ID<=5
Hibernate: select bigque0_.ID as ID51_0_, bigque0_.Content as Content51_0_, bigque0_.SubjectID as SubjectID51_0_ from cloudroom.bigque bigque0_ where bigque0_.ID=?
选择题
Hibernate: select bigque0_.ID as ID51_0_, bigque0_.Content as Content51_0_, bigque0_.SubjectID as SubjectID51_0_ from cloudroom.bigque bigque0_ where bigque0_.ID=?
填空题
Hibernate: select bigque0_.ID as ID51_0_, bigque0_.Content as Content51_0_, bigque0_.SubjectID as SubjectID51_0_ from cloudroom.bigque bigque0_ where bigque0_.ID=?
阅读题
Hibernate: select bigque0_.ID as ID51_0_, bigque0_.Content as Content51_0_, bigque0_.SubjectID as SubjectID51_0_ from cloudroom.bigque bigque0_ where bigque0_.ID=?
判断题
Hibernate: select bigque0_.ID as ID51_0_, bigque0_.Content as Content51_0_, bigque0_.SubjectID as SubjectID51_0_ from cloudroom.bigque bigque0_ where bigque0_.ID=?
选择题
===========
Hibernate: select bigque0_.ID as col_0_0_ from cloudroom.bigque bigque0_ where bigque0_.ID<=6
选择题
填空题
阅读题
判断题
选择题
Hibernate: select bigque0_.ID as ID51_0_, bigque0_.Content as Content51_0_, bigque0_.SubjectID as SubjectID51_0_ from cloudroom.bigque bigque0_ where bigque0_.ID=?
填空题


结论:

延迟加载 先发送一条sql语句获取所有的id  用到的时候再发送SQL去数据库查找每个对象


在一个session中两次iterate   第二个iterate还是会发送一条SQL获取id    用到的时候再发送SQL去数据库查找每个对象(如果session中有这个对象,就在缓存中查找,不发送SQL)

 


list:

                String sql = "from Bigque where id <= 5";
	//	String sql2 = "from Bigque where id <= 6";
		BigqueDAO dao = new BigqueDAO();
		Session session = dao.getSession();
		List<Bigque> list = session.createQuery(sql).list();
		for(int i=0; i<list.size(); i++){
			System.out.println(list.get(i).getContent());
		}
		System.out.println("=============");
		
		List<Bigque> list2 = session.createQuery(sql).list();
		for(int i=0; i<list2.size(); i++){
			System.out.println(list2.get(i).getContent());
		}
		session.close();
结果:
Hibernate: select bigque0_.ID as ID51_, bigque0_.Content as Content51_, bigque0_.SubjectID as SubjectID51_ from cloudroom.bigque bigque0_ where bigque0_.ID<=5
选择题
填空题
阅读题
判断题
选择题
=============
Hibernate: select bigque0_.ID as ID51_, bigque0_.Content as Content51_, bigque0_.SubjectID as SubjectID51_ from cloudroom.bigque bigque0_ where bigque0_.ID<=5
选择题
填空题
阅读题
判断题
选择题
结论:list取所有  第二次发出仍会到数据库查找


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值