本文主要是介绍Hibernate之查询中get()和load()的区别,list()和iterate()的区别,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
//一次性把数据对象取出来@Testpublic 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 - selectperson0_.id as id1_,person0_.p_age as p2_1_,person0_.group_id as group4_1_,person0_.p_name as p3_1_ fromp_person person0_
张三0----1
张三1----2
张三2----3
张三3----4
张三4----5
张三5----6
张三6----7
张三7----8
张三8----9
张三9----10
//一次性取出来的是所有对象的主键值,并且会把这些主键值放在Session缓冲中去//下次需要的话,直接在Session中获取,不需要再次访问数据库//缺点是将查询出来的主键值,分别再以主键进行取值对象,造成数据库压力巨大@Testpublic 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 - selectperson0_.id as col_0_0_ fromp_person person0_
16:03:55,806 DEBUG SQL:111 - selectperson0_.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_ fromp_person person0_ whereperson0_.id=?
张三0
16:03:55,823 DEBUG SQL:111 - selectperson0_.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_ fromp_person person0_ whereperson0_.id=?
张三1
16:03:55,826 DEBUG SQL:111 - selectperson0_.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_ fromp_person person0_ whereperson0_.id=?
张三2
16:03:55,832 DEBUG SQL:111 - selectperson0_.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_ fromp_person person0_ whereperson0_.id=?
张三3
16:03:55,836 DEBUG SQL:111 - selectperson0_.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_ fromp_person person0_ whereperson0_.id=?
张三4
16:03:55,838 DEBUG SQL:111 - selectperson0_.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_ fromp_person person0_ whereperson0_.id=?
张三5
16:03:55,841 DEBUG SQL:111 - selectperson0_.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_ fromp_person person0_ whereperson0_.id=?
张三6
16:03:55,844 DEBUG SQL:111 - selectperson0_.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_ fromp_person person0_ whereperson0_.id=?
张三7
16:03:55,846 DEBUG SQL:111 - selectperson0_.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_ fromp_person person0_ whereperson0_.id=?
张三8
16:03:55,875 DEBUG SQL:111 - selectperson0_.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_ fromp_person person0_ whereperson0_.id=?
张三9
get()查询
//立刻发送sql语句,不管我们是否需要,比如person.getName();@Testpublic 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();@Testpublic void findTestload(){Session s=sessionFactory.getCurrentSession();s.beginTransaction();Person person=(Person)s.load(Person.class, 1);
// System.out.println(person.getName());s.getTransaction().commit();}
这篇关于Hibernate之查询中get()和load()的区别,list()和iterate()的区别的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!