码迷,mamicode.com
首页 > Web开发 > 详细

Hibernate查询优化——类级别查询(集合策略)

时间:2020-01-12 00:09:15      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:文件的   管理   抓取策略   结果   print   out   获取   single   zha   

1、类级别查询:

get方法和load方法:

(1)get方法:

 public static void testSel() {
        Session session = HibernateUtils.openSession();
        Transaction transaction = session.beginTransaction();
        Student student=session.get(Student.class,201811);
        System.out.println(student);
        transaction.commit();
        session.close();
    }

特点:执行get方法时,立即发送查询语句查询结果。

(2)load方法(延迟加载,即:只有在使用的时候才会进行查询):

 public static void testSel() {
        Session session = HibernateUtils.openSession();
        Transaction transaction = session.beginTransaction();
        Student student=session.load(Student.class,201811);
        System.out.println(student);
        transaction.commit();
        session.close();
    }

特点:在执行load方法时,不会发送任何sql语句,不会产生任何查询结果,但是会产生一个对象,在使用该对象时才会产生查询结果。

延迟加载与配置文件的关系:

延迟加载(默认):

<class name="Student" table="student" lazy="true">

不延迟加载:

<class name="Student" table="student" lazy="false">

延迟加载可以提高效率(对象取出来可以不用,到真正需要的时候才进行查询)。

 

2、关联级别延迟加载和抓取策略:

集合级别的关联:

lazy属性:决定是否延迟加载
    true(默认值):延迟加载,懒惰加载
    false:立即加载
    extra:极其懒惰

fetch属性:决定加载策略,使用什么类型的sql语句加载集合数据
    select(默认值):单表查询加载
    join:多表查询加载集合
    subselect:使用子查询加载集合

(1)lazy=true,fetch=select

  public static void testSel() {
        Session session = HibernateUtils.openSession();
        Transaction transaction = session.beginTransaction();
        Student student=session.load(Student.class,201811);//不执行查询
        Clas clas= student.getaClas();
        System.out.println(clas);//执行查询
        transaction.commit();
        session.close();
    }
<hibernate-mapping package="pers.zhb.domain">
    <class name="Clas" table="class">
        <id name="classno" column="classno">
            <generator class="native"></generator>
        </id><!--主键-->
            <property name="department" column="department"></property>
            <property name="monitor" column="monitor"></property>
            <property name="classname" column="classname"></property>
        <set name="students" table="student" lazy="true" fetch="select"><!--一对多关系配置-->
            <key column="classno" update="false" ></key><!--指定了集合表的外键-->
            <one-to-many class="Student"></one-to-many>
        </set>
    </class>
</hibernate-mapping>

技术图片

 

 表中的数据:

class表:

技术图片

 

 student表:

技术图片

 

 (2)lazy=false,fetch=select

   public static void testSel() {
        Session session = HibernateUtils.openSession();
        Transaction transaction = session.beginTransaction();
        Student student=session.load(Student.class,201811);//没有使用即开始执行查询
        Clas clas= student.getaClas();
        System.out.println(clas);
        transaction.commit();
        session.close();
    }

技术图片

 

 技术图片

 

 (3)lazy=extra,fetch=select

极其懒惰与懒加载几乎一致。

技术图片

 

 技术图片

 

 获取查询结果的size:

public class Test{
    public static void testSel() {
        Session session = HibernateUtils.openSession();
        Transaction transaction = session.beginTransaction();
        Student student=session.load(Student.class,201811);
        Set<Clas> clas= Collections.singleton(student.getaClas());
        System.out.println(clas);
        System.out.println(clas.size());
        transaction.commit();
        session.close();
    }
Hibernate: 
    select
        student0_.studentno as studentn1_1_0_,
        student0_.birthday as birthday2_1_0_,
        student0_.classno as classno3_1_0_,
        student0_.phone as phone4_1_0_,
        student0_.sex as sex5_1_0_,
        student0_.sname as sname6_1_0_,
        student0_.point as point7_1_0_ 
    from
        student student0_ 
    where
        student0_.studentno=?
Hibernate: 
    select
        clas0_.classno as classno1_0_0_,
        clas0_.department as departme2_0_0_,
        clas0_.monitor as monitor3_0_0_,
        clas0_.classname as classnam4_0_0_ 
    from
        class clas0_ 
    where
        clas0_.classno=?
Hibernate: 
    select
        students0_.classno as classno3_1_0_,
        students0_.studentno as studentn1_1_0_,
        students0_.studentno as studentn1_1_1_,
        students0_.birthday as birthday2_1_1_,
        students0_.classno as classno3_1_1_,
        students0_.phone as phone4_1_1_,
        students0_.sex as sex5_1_1_,
        students0_.sname as sname6_1_1_,
        students0_.point as point7_1_1_ 
    from
        student students0_ 
    where
        students0_.classno=?
[Clas{classno=tx171, department=信工学院, monitor=张伟, classname=通信171, 
students=[Student{studentno=201811, sname=zhai, sex=, birthday=1998-11-11, classno=tx171, point=890.0, phone=19837372532},
Student{studentno=201813, sname=zhai3, sex=, birthday=1998-11-11, classno=tx171, point=892.0, phone=19837372534},
Student{studentno=201812, sname=zhai2, sex=, birthday=1998-11-11, classno=tx171, point=893.0, phone=19837372533}]}] 1

(4)fetch=select

         当fetch="select" 时lazy无论是 true | false | extra都失效,都是立即加载。

Hibernate: 
    select
        student0_.studentno as studentn1_1_0_,
        student0_.birthday as birthday2_1_0_,
        student0_.classno as classno3_1_0_,
        student0_.phone as phone4_1_0_,
        student0_.sex as sex5_1_0_,
        student0_.sname as sname6_1_0_,
        student0_.point as point7_1_0_ 
    from
        student student0_ 
    where
        student0_.studentno=?
Hibernate: 
    select
        clas0_.classno as classno1_0_0_,
        clas0_.department as departme2_0_0_,
        clas0_.monitor as monitor3_0_0_,
        clas0_.classname as classnam4_0_0_ 
    from
        class clas0_ 
    where
        clas0_.classno=?
Hibernate: 
    select
        students0_.classno as classno3_1_0_,
        students0_.studentno as studentn1_1_0_,
        students0_.studentno as studentn1_1_1_,
        students0_.birthday as birthday2_1_1_,
        students0_.classno as classno3_1_1_,
        students0_.phone as phone4_1_1_,
        students0_.sex as sex5_1_1_,
        students0_.sname as sname6_1_1_,
        students0_.point as point7_1_1_ 
    from
        student students0_ 
    where
        students0_.classno=?
[Clas{classno=tx171, department=信工学院, monitor=张伟, classname=通信171, 
students=[Student{studentno=201811, sname=zhai, sex=, birthday=1998-11-11, classno=tx171, point=890.0, phone=19837372532},
Student{studentno=201813, sname=zhai3, sex=, birthday=1998-11-11, classno=tx171, point=892.0, phone=19837372534},
Student{studentno=201812, sname=zhai2, sex=, birthday=1998-11-11, classno=tx171, point=893.0, phone=19837372533}]}]

(5)lazy="true" fetch="subselect"

Hibernate: 
    select
        student0_.studentno as studentn1_1_0_,
        student0_.birthday as birthday2_1_0_,
        student0_.classno as classno3_1_0_,
        student0_.phone as phone4_1_0_,
        student0_.sex as sex5_1_0_,
        student0_.sname as sname6_1_0_,
        student0_.point as point7_1_0_ 
    from
        student student0_ 
    where
        student0_.studentno=?
Hibernate: 
    select
        clas0_.classno as classno1_0_0_,
        clas0_.department as departme2_0_0_,
        clas0_.monitor as monitor3_0_0_,
        clas0_.classname as classnam4_0_0_ 
    from
        class clas0_ 
    where
        clas0_.classno=?
Hibernate: 
    select
        students0_.classno as classno3_1_0_,
        students0_.studentno as studentn1_1_0_,
        students0_.studentno as studentn1_1_1_,
        students0_.birthday as birthday2_1_1_,
        students0_.classno as classno3_1_1_,
        students0_.phone as phone4_1_1_,
        students0_.sex as sex5_1_1_,
        students0_.sname as sname6_1_1_,
        students0_.point as point7_1_1_ 
    from
        student students0_ 
    where
        students0_.classno=?
[Clas{classno=tx171, department=信工学院, monitor=张伟, classname=通信171,
students=[Student{studentno=201811, sname=zhai, sex=, birthday=1998-11-11, classno=tx171, point=890.0, phone=19837372532},
Student{studentno=201813, sname=zhai3, sex=, birthday=1998-11-11, classno=tx171, point=892.0, phone=19837372534},
Student{studentno=201812, sname=zhai2, sex=, birthday=1998-11-11, classno=tx171, point=893.0, phone=19837372533}]}]

(6)lazy="true" fetch="subselect"

Hibernate: 
    select
        clas0_.classno as classno1_0_,
        clas0_.department as departme2_0_,
        clas0_.monitor as monitor3_0_,
        clas0_.classname as classnam4_0_ 
    from
        class clas0_
Hibernate: 
    select
        students0_.classno as classno3_1_1_,
        students0_.studentno as studentn1_1_1_,
        students0_.studentno as studentn1_1_0_,
        students0_.birthday as birthday2_1_0_,
        students0_.classno as classno3_1_0_,
        students0_.phone as phone4_1_0_,
        students0_.sex as sex5_1_0_,
        students0_.sname as sname6_1_0_,
        students0_.point as point7_1_0_ 
    from
        student students0_ 
    where
        students0_.classno in (
            select
                clas0_.classno 
            from
                class clas0_
        )
Clas{classno=80501, department=计算机学院, monitor=刘国平, classname=计算机0801, students=[]}
[]
0
Clas{classno=80601, department=机械学院, monitor=王善执, classname=机械0801, students=[]}
[]
0
Clas{classno=90501, department=计算机学院, monitor=马文斐, classname=计算机0901, students=[]}
[]
0
Clas{classno=90502, department=计算机学院, monitor=章成楠, classname=计算机0902, students=[]}
[]
0
Clas{classno=90801, department=管理学院, monitor=党海, classname=管理0901, students=[]}
[]
0
Clas{classno=90802, department=管理学院, monitor=张晓, classname=管理0802, students=[]}
[]
0
Clas{classno=tx161, department=信息工程学院, monitor=张丽, classname=通信161, students=[]}
[]
0
Clas{classno=tx172, department=信工学院, monitor=李月, classname=通信172, students=[]}
[]
0
Clas{classno=tx171, department=信工学院, monitor=张伟, classname=通信171, students=[Student{studentno=201811, sname=zhai, sex=, birthday=1998-11-11, classno=tx171, point=890.0, phone=19837372532}, Student{studentno=201813, sname=zhai3, sex=, birthday=1998-11-11, classno=tx171, point=892.0, phone=19837372534}, Student{studentno=201812, sname=zhai2, sex=, birthday=1998-11-11, classno=tx171, point=893.0, phone=19837372533}]}
[Student{studentno=201811, sname=zhai, sex=, birthday=1998-11-11, classno=tx171, point=890.0, phone=19837372532}, Student{studentno=201813, sname=zhai3, sex=, birthday=1998-11-11, classno=tx171, point=892.0, phone=19837372534}, Student{studentno=201812, sname=zhai2, sex=, birthday=1998-11-11, classno=tx171, point=893.0, phone=19837372533}]
3

(7)lazy="false" fetch="subselect"

Hibernate: 
    select
        clas0_.classno as classno1_0_,
        clas0_.department as departme2_0_,
        clas0_.monitor as monitor3_0_,
        clas0_.classname as classnam4_0_ 
    from
        class clas0_
Hibernate: 
    select
        students0_.classno as classno3_1_1_,
        students0_.studentno as studentn1_1_1_,
        students0_.studentno as studentn1_1_0_,
        students0_.birthday as birthday2_1_0_,
        students0_.classno as classno3_1_0_,
        students0_.phone as phone4_1_0_,
        students0_.sex as sex5_1_0_,
        students0_.sname as sname6_1_0_,
        students0_.point as point7_1_0_ 
    from
        student students0_ 
    where
        students0_.classno in (
            select
                clas0_.classno 
            from
                class clas0_
        )
Clas{classno=80501, department=计算机学院, monitor=刘国平, classname=计算机0801, students=[]}
[]
0
Clas{classno=80601, department=机械学院, monitor=王善执, classname=机械0801, students=[]}
[]
0
Clas{classno=90501, department=计算机学院, monitor=马文斐, classname=计算机0901, students=[]}
[]
0
Clas{classno=90502, department=计算机学院, monitor=章成楠, classname=计算机0902, students=[]}
[]
0
Clas{classno=90801, department=管理学院, monitor=党海, classname=管理0901, students=[]}
[]
0
Clas{classno=90802, department=管理学院, monitor=张晓, classname=管理0802, students=[]}
[]
0
Clas{classno=tx161, department=信息工程学院, monitor=张丽, classname=通信161, students=[]}
[]
0
Clas{classno=tx172, department=信工学院, monitor=李月, classname=通信172, students=[]}
[]
0
Clas{classno=tx171, department=信工学院, monitor=张伟, classname=通信171, students=[Student{studentno=201813, sname=zhai3, sex=, birthday=1998-11-11, classno=tx171, point=892.0, phone=19837372534}, Student{studentno=201811, sname=zhai, sex=, birthday=1998-11-11, classno=tx171, point=890.0, phone=19837372532}, Student{studentno=201812, sname=zhai2, sex=, birthday=1998-11-11, classno=tx171, point=893.0, phone=19837372533}]}
[Student{studentno=201813, sname=zhai3, sex=, birthday=1998-11-11, classno=tx171, point=892.0, phone=19837372534}, Student{studentno=201811, sname=zhai, sex=, birthday=1998-11-11, classno=tx171, point=890.0, phone=19837372532}, Student{studentno=201812, sname=zhai2, sex=, birthday=1998-11-11, classno=tx171, point=893.0, phone=19837372533}]
3

(8)lazy="extra" fetch="subselect"

Hibernate: 
    select
        clas0_.classno as classno1_0_,
        clas0_.department as departme2_0_,
        clas0_.monitor as monitor3_0_,
        clas0_.classname as classnam4_0_ 
    from
        class clas0_
Hibernate: 
    select
        students0_.classno as classno3_1_1_,
        students0_.studentno as studentn1_1_1_,
        students0_.studentno as studentn1_1_0_,
        students0_.birthday as birthday2_1_0_,
        students0_.classno as classno3_1_0_,
        students0_.phone as phone4_1_0_,
        students0_.sex as sex5_1_0_,
        students0_.sname as sname6_1_0_,
        students0_.point as point7_1_0_ 
    from
        student students0_ 
    where
        students0_.classno in (
            select
                clas0_.classno 
            from
                class clas0_
        )
Clas{classno=80501, department=计算机学院, monitor=刘国平, classname=计算机0801, students=[]}
[]
0
Clas{classno=80601, department=机械学院, monitor=王善执, classname=机械0801, students=[]}
[]
0
Clas{classno=90501, department=计算机学院, monitor=马文斐, classname=计算机0901, students=[]}
[]
0
Clas{classno=90502, department=计算机学院, monitor=章成楠, classname=计算机0902, students=[]}
[]
0
Clas{classno=90801, department=管理学院, monitor=党海, classname=管理0901, students=[]}
[]
0
Clas{classno=90802, department=管理学院, monitor=张晓, classname=管理0802, students=[]}
[]
0
Clas{classno=tx161, department=信息工程学院, monitor=张丽, classname=通信161, students=[]}
[]
0
Clas{classno=tx172, department=信工学院, monitor=李月, classname=通信172, students=[]}
[]
0
Clas{classno=tx171, department=信工学院, monitor=张伟, classname=通信171, 
students=[Student{studentno=201811, sname=zhai, sex=, birthday=1998-11-11, classno=tx171, point=890.0, phone=19837372532},
Student{studentno=201813, sname=zhai3, sex=, birthday=1998-11-11, classno=tx171, point=892.0, phone=19837372534},
Student{studentno=201812, sname=zhai2, sex=, birthday=1998-11-11, classno=tx171, point=893.0, phone=19837372533}]} [Student{studentno=201811, sname=zhai, sex=, birthday=1998-11-11, classno=tx171, point=890.0, phone=19837372532},
Student{studentno=201813, sname=zhai3, sex=, birthday=1998-11-11, classno=tx171, point=892.0, phone=19837372534},
Student{studentno=201812, sname=zhai2, sex=, birthday=1998-11-11, classno=tx171, point=893.0, phone=19837372533}] 3

Hibernate查询优化——类级别查询(集合策略)

标签:文件的   管理   抓取策略   结果   print   out   获取   single   zha   

原文地址:https://www.cnblogs.com/zhai1997/p/11980053.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!