首页 > 代码库 > Hibernate之查询中get()和load()的区别,list()和iterate()的区别
Hibernate之查询中get()和load()的区别,list()和iterate()的区别
【Hibernate】之查询中get()和load()的区别,list()和iterate()的区别
list()查询
//一次性把数据对象取出来 @Test public void findTestList(){ Session s=sessionFactory.getCurrentSession(); s.beginTransaction(); List<Person> persons=s.createQuery("from Person").list(); for(Person person:persons){ System.out.println(person.getName()+"----"+person.getId()); } s.getTransaction().commit(); }
sql语句如下:
16:00:43,118 DEBUG SQL:111 - select person0_.id as id1_, person0_.p_age as p2_1_, person0_.group_id as group4_1_, person0_.p_name as p3_1_ from p_person person0_ 张三0----1 张三1----2 张三2----3 张三3----4 张三4----5 张三5----6 张三6----7 张三7----8 张三8----9 张三9----10
iterate()查询
//一次性取出来的是所有对象的主键值,并且会把这些主键值放在Session缓冲中去 //下次需要的话,直接在Session中获取,不需要再次访问数据库 //缺点是将查询出来的主键值,分别再以主键进行取值对象,造成数据库压力巨大 @Test public void findTestIterate(){ Session s=sessionFactory.getCurrentSession(); s.beginTransaction(); Iterator<Person> persons=s.createQuery("from Person").iterate(); while(persons.hasNext()){ System.out.println(persons.next().getName()); } s.getTransaction().commit(); }
sql语句
16:03:55,776 DEBUG SQL:111 - select person0_.id as col_0_0_ from p_person person0_ 16:03:55,806 DEBUG SQL:111 - select person0_.id as id1_0_, person0_.p_age as p2_1_0_, person0_.group_id as group4_1_0_, person0_.p_name as p3_1_0_ from p_person person0_ where person0_.id=? 张三0 16:03:55,823 DEBUG SQL:111 - select person0_.id as id1_0_, person0_.p_age as p2_1_0_, person0_.group_id as group4_1_0_, person0_.p_name as p3_1_0_ from p_person person0_ where person0_.id=? 张三1 16:03:55,826 DEBUG SQL:111 - select person0_.id as id1_0_, person0_.p_age as p2_1_0_, person0_.group_id as group4_1_0_, person0_.p_name as p3_1_0_ from p_person person0_ where person0_.id=? 张三2 16:03:55,832 DEBUG SQL:111 - select person0_.id as id1_0_, person0_.p_age as p2_1_0_, person0_.group_id as group4_1_0_, person0_.p_name as p3_1_0_ from p_person person0_ where person0_.id=? 张三3 16:03:55,836 DEBUG SQL:111 - select person0_.id as id1_0_, person0_.p_age as p2_1_0_, person0_.group_id as group4_1_0_, person0_.p_name as p3_1_0_ from p_person person0_ where person0_.id=? 张三4 16:03:55,838 DEBUG SQL:111 - select person0_.id as id1_0_, person0_.p_age as p2_1_0_, person0_.group_id as group4_1_0_, person0_.p_name as p3_1_0_ from p_person person0_ where person0_.id=? 张三5 16:03:55,841 DEBUG SQL:111 - select person0_.id as id1_0_, person0_.p_age as p2_1_0_, person0_.group_id as group4_1_0_, person0_.p_name as p3_1_0_ from p_person person0_ where person0_.id=? 张三6 16:03:55,844 DEBUG SQL:111 - select person0_.id as id1_0_, person0_.p_age as p2_1_0_, person0_.group_id as group4_1_0_, person0_.p_name as p3_1_0_ from p_person person0_ where person0_.id=? 张三7 16:03:55,846 DEBUG SQL:111 - select person0_.id as id1_0_, person0_.p_age as p2_1_0_, person0_.group_id as group4_1_0_, person0_.p_name as p3_1_0_ from p_person person0_ where person0_.id=? 张三8 16:03:55,875 DEBUG SQL:111 - select person0_.id as id1_0_, person0_.p_age as p2_1_0_, person0_.group_id as group4_1_0_, person0_.p_name as p3_1_0_ from p_person person0_ where person0_.id=? 张三9
get()查询
//立刻发送sql语句,不管我们是否需要,比如person.getName(); @Test public void findTestget(){ Session s=sessionFactory.getCurrentSession(); s.beginTransaction(); Person person=(Person)s.get(Person.class, 1); // System.out.println(person.getName()); s.getTransaction().commit(); }
load()查询
//不会立刻发送sql语句,只有当我们需要的时候才会发送sql语句,比如person.getName(); @Test public void findTestload(){ Session s=sessionFactory.getCurrentSession(); s.beginTransaction(); Person person=(Person)s.load(Person.class, 1); // System.out.println(person.getName()); s.getTransaction().commit(); }
本文出自 “诺言永远依恋小柴、、、” 博客,请务必保留此出处http://1936625305.blog.51cto.com/6410597/1576128
Hibernate之查询中get()和load()的区别,list()和iterate()的区别
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。