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();
    }



list()和Iterator()不同之处:
1、list()查询所有
2、Iterator()查主键
3、list()每次查询都访问数据库
4、Iterator()每次查询先访问Session缓冲,存在就直接获取,不存在再查询数据库


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。