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

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

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

时间: 2024-10-11 16:05:34

Hibernate查询优化——类级别查询(集合策略)的相关文章

Hibernate注解-类级别注解

hibernate(八) Hibernate检索策略(类级别,关联级别,批量检索)详解

序言 很多看起来很难的东西其实并不难,关键是看自己是否花费了时间和精力去看,如果一个东西你能看得懂,同样的,别人也能看得懂,体现不出和别人的差距,所以当你觉得自己看了很多书或者学了很多东西的时候,你要想想,你花费的也就那么一点时间,别人花你这么多时间也能够学到你所学到的东西,所以还是要继续努力.既然不是天才,唯有靠勤奋来弥补. --WH 一.概述 检索策略分三大块,类级别检索策略和关联级别检测策略. 类级别检索策略:get.load. 关联级别检索策略:order.getCustomer().g

(转) Hibernate持久化类与主键生成策略

http://blog.csdn.net/yerenyuan_pku/article/details/65462930 Hibernate持久化类 什么是持久化类呢?在Hibernate中持久化类的英文名称是Persistent Object(简称PO),PO=POJO+hbm映射配置文件. 对于Hibernate中的PO,有如下编写规则: 必须提供一个无参数的public构造方法. 所有属性要用private修饰,对外提供public的get/set方法. 在PO类必须提供一个标识属性,让它与

Hibernate支持类中的分页查询的实现

Hibernate支持类的实现 package com.myHibernateDao; import java.sql.SQLException; import java.util.List; import javax.annotation.Resource; import org.hibernate.HibernateException; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate

Hibernate中的条件查询完毕类

Hibernate中的条件查询有下面三个类完毕: 1.Criteria:代表一次查询 2.Criterion:代表一个查询条件 3.Restrictions:产生查询条件的工具类

Hibernate中的条件查询完成类

Hibernate中的条件查询有以下三个类完成: 1.Criteria:代表一次查询 2.Criterion:代表一个查询条件 3.Restrictions:产生查询条件的工具类 Hibernate中的条件查询完成类

hibernate的延迟加载和抓取策略

一,延迟加载 1.实体类延迟加载 通过代理机制完成,由javassist类库实现运行时代理,修改实体类的字节码实现了运行时代理     <class lazy="true|false">     实体级别的延迟加载默认值为true,意味实体对象是延迟加载,只影响load方法.      <class lazy="true|false">其他查询方式都是立即加载              2.关联属性延迟加载 默认情况下除了<one-to

Hibernate关联操作、查询操作、高级特性、并发处理机制

本文所需的数据库初始文件,Hibernate常用操作的完整示例代码(包含所有Hibernate操作所需jar文件)提供下载学习:http://download.csdn.net/detail/daijin888888/9551724 1.Hibernate关联映射 1)什么是关联映射? 如果表之间具有关联关系,Hibernate允许我们在hbm.xml中描述他们的关联关系,然后在我们操作其中一张表时,自动的根据这种关系操作到另外的关系表,那么这种关联关系的设置,我们称之为关联映射. 2)关联映射

Spring Hibernate JPA 联表查询 复杂查询

(转自:http://www.cnblogs.com/jiangxiaoyaoblog/p/5635152.html) 今天刷网,才发现: 1)如果想用hibernate注解,是不是一定会用到jpa的? 是.如果hibernate认为jpa的注解够用,就直接用.否则会弄一个自己的出来作为补充. 2)jpa和hibernate都提供了Entity,我们应该用哪个,还是说可以两个一起用? Hibernate的Entity是继承了jpa的,所以如果觉得jpa的不够用,直接使用hibernate的即可