JavaWeb学习:Hibernate抓取策略

Hibernate性能优化的手段:缓存、抓取策略

抓取策略不会单独使用,抓取策略通常和延迟加载一起使用

一、延迟加载

  ①、延迟加载:lazy(懒加载)。执行到该行代码的时候,不会发送语句去进行查询,在真正使用这个对象的属性的时候才会发送SQL语句进行查询。

  ②、延迟加载的分类

    2.1、类级别的延迟加载

        指的是通过load方法查询某个对象的时候,是否采用延迟。session.load(Customer.class,1l);

        2.1.1、类级别延迟加载失效

          2.1.1.1、*.hbm.xml修改class 的lazy的值

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <!-- 建立类与表的映射 -->
    <class name="com.hibernate.domain.Customer" table="cst_customer" lazy="false">

          2.1.1.2、将持久化类使用final修饰

          2.1.1.3、Hibernate. Initialize()

    @Test
    public void demo12() {
    Session session = HibernateUtils.getCurrentSession();
    Transaction transaction = session.beginTransaction();

    Customer customer=session.load(Customer.class, 1l);
    Hibernate.initialize(customer);
    
    transaction.commit();
    }

    2.2、关联级别的延迟加载

        指的是在查询到某个对象的时候,查询其关联的对象的时候,是否采用延迟加载。

          Customer customer = session.get(Customer.class,1l);

          customer.getLinkMans();----通过客户获得联系人的时候,联系人对象是否采用了延迟加载,称为是关联级别的延迟。

        抓取策略往往会和关联级别的延迟加载一起使用,优化语句。

       2.2.1、抓取策略的概述

        通过一个对象抓取到关联对象需要发送SQL语句,通过配置<set>或者<many-to-one>上的fetch和lazy属性优化发送的SQL语句

      2.2.2、<set>上的fetch和lazy

      • fetch:抓取策略,控制SQL语句格式
        • select:默认值,发送普通的select语句,查询关联对象
        • join:发送一条迫切左外连接查询关联对象
        • subselect:发送一条子查询查询其关联对象
      • lazy:延迟加载,控制查询关联对象的时候是否采用延迟
        • true:默认值,查询关联对象的时候,采用延迟加载
        • false:查询关联对象的时候,不采用延迟加载
        • extra :极其懒惰。
      • 在实际开发中,一般都采用默认值。如果有特殊的需求,可能需要配置join

        Java代码:    

    @Test
    public void demo5() {
    Session session = HibernateUtils.getCurrentSession();
    Transaction transaction = session.beginTransaction();

    Customer customer=session.get(Customer.class, 2l);
    
    System.out.println(customer.getLinkMans().size());//加断点
    transaction.commit();
    }

        2.2.2.1、 fetch="select" lazy="true"

     <!-- set标签 :
         * name :多的一方的对象集合的属性名称。 
         * cascade:级联
         * inverse:放弃外键维护权。 -->
        <set name="linkMans" cascade="save-update,delete" inverse="true" fetch="select" lazy="true" >

        执行到断点前结果:

Hibernate: 
    select
        customer0_.cust_id as cust_id1_0_0_,
        customer0_.cust_name as cust_nam2_0_0_,
        customer0_.cust_source as cust_sou3_0_0_,
        customer0_.cust_industry as cust_ind4_0_0_,
        customer0_.cust_level as cust_lev5_0_0_,
        customer0_.cust_phone as cust_pho6_0_0_,
        customer0_.cust_mobile as cust_mob7_0_0_ 
    from
        cst_customer customer0_ 
    where
        customer0_.cust_id=?

        执行到断点后结果:

Hibernate: 
    select
        linkmans0_.lkm_cust_id as lkm_cus10_1_0_,
        linkmans0_.lkm_id as lkm_id1_1_0_,
        linkmans0_.lkm_id as lkm_id1_1_1_,
        linkmans0_.lkm_name as lkm_name2_1_1_,
        linkmans0_.lkm_gender as lkm_gend3_1_1_,
        linkmans0_.lkm_phone as lkm_phon4_1_1_,
        linkmans0_.lkm_mobile as lkm_mobi5_1_1_,
        linkmans0_.lkm_email as lkm_emai6_1_1_,
        linkmans0_.lkm_qq as lkm_qq7_1_1_,
        linkmans0_.lkm_position as lkm_posi8_1_1_,
        linkmans0_.lkm_memo as lkm_memo9_1_1_,
        linkmans0_.lkm_cust_id as lkm_cus10_1_1_ 
    from
        cst_linkman linkmans0_ 
    where
        linkmans0_.lkm_cust_id=?
1

        2.2.2.2、 fetch="select" lazy="false"

<set name="linkMans" cascade="save-update,delete" inverse="true" fetch="select" lazy="false" >

        执行到断点前结果:

Hibernate: 
    select
        customer0_.cust_id as cust_id1_0_0_,
        customer0_.cust_name as cust_nam2_0_0_,
        customer0_.cust_source as cust_sou3_0_0_,
        customer0_.cust_industry as cust_ind4_0_0_,
        customer0_.cust_level as cust_lev5_0_0_,
        customer0_.cust_phone as cust_pho6_0_0_,
        customer0_.cust_mobile as cust_mob7_0_0_ 
    from
        cst_customer customer0_ 
    where
        customer0_.cust_id=?
Hibernate: 
    select
        linkmans0_.lkm_cust_id as lkm_cus10_1_0_,
        linkmans0_.lkm_id as lkm_id1_1_0_,
        linkmans0_.lkm_id as lkm_id1_1_1_,
        linkmans0_.lkm_name as lkm_name2_1_1_,
        linkmans0_.lkm_gender as lkm_gend3_1_1_,
        linkmans0_.lkm_phone as lkm_phon4_1_1_,
        linkmans0_.lkm_mobile as lkm_mobi5_1_1_,
        linkmans0_.lkm_email as lkm_emai6_1_1_,
        linkmans0_.lkm_qq as lkm_qq7_1_1_,
        linkmans0_.lkm_position as lkm_posi8_1_1_,
        linkmans0_.lkm_memo as lkm_memo9_1_1_,
        linkmans0_.lkm_cust_id as lkm_cus10_1_1_ 
    from
        cst_linkman linkmans0_ 
    where
        linkmans0_.lkm_cust_id=?

        执行到断点后结果:1

        2.2.2.3、 fetch="select" lazy="extra"

<set name="linkMans" cascade="save-update,delete" inverse="true" fetch="select" lazy="extra" >

        执行到断点前结果:

Hibernate: 
    select
        customer0_.cust_id as cust_id1_0_0_,
        customer0_.cust_name as cust_nam2_0_0_,
        customer0_.cust_source as cust_sou3_0_0_,
        customer0_.cust_industry as cust_ind4_0_0_,
        customer0_.cust_level as cust_lev5_0_0_,
        customer0_.cust_phone as cust_pho6_0_0_,
        customer0_.cust_mobile as cust_mob7_0_0_ 
    from
        cst_customer customer0_ 
    where
        customer0_.cust_id=?

        执行到断点后结果:

Hibernate: 
    select
        count(lkm_id) 
    from
        cst_linkman 
    where
        lkm_cust_id =?
1

        2.2.2.4、 fetch="join" lazy=失效

<set name="linkMans" cascade="save-update,delete" inverse="true" fetch="join" lazy="true" >

        执行到断点前结果:

Hibernate: 
    select
        customer0_.cust_id as cust_id1_0_0_,
        customer0_.cust_name as cust_nam2_0_0_,
        customer0_.cust_source as cust_sou3_0_0_,
        customer0_.cust_industry as cust_ind4_0_0_,
        customer0_.cust_level as cust_lev5_0_0_,
        customer0_.cust_phone as cust_pho6_0_0_,
        customer0_.cust_mobile as cust_mob7_0_0_,
        linkmans1_.lkm_cust_id as lkm_cus10_1_1_,
        linkmans1_.lkm_id as lkm_id1_1_1_,
        linkmans1_.lkm_id as lkm_id1_1_2_,
        linkmans1_.lkm_name as lkm_name2_1_2_,
        linkmans1_.lkm_gender as lkm_gend3_1_2_,
        linkmans1_.lkm_phone as lkm_phon4_1_2_,
        linkmans1_.lkm_mobile as lkm_mobi5_1_2_,
        linkmans1_.lkm_email as lkm_emai6_1_2_,
        linkmans1_.lkm_qq as lkm_qq7_1_2_,
        linkmans1_.lkm_position as lkm_posi8_1_2_,
        linkmans1_.lkm_memo as lkm_memo9_1_2_,
        linkmans1_.lkm_cust_id as lkm_cus10_1_2_ 
    from
        cst_customer customer0_ 
    left outer join
        cst_linkman linkmans1_ 
            on customer0_.cust_id=linkmans1_.lkm_cust_id 
    where
        customer0_.cust_id=?

        执行到断点后结果:1

        2.2.2.5、 fetch="subselect" lazy="true"

    @Test
    public void demo5() {
    Session session = HibernateUtils.getCurrentSession();
    Transaction transaction = session.beginTransaction();

    List<Customer> list = session.createCriteria(Customer.class).list();
    for (Customer customer : list) {
        System.out.println(customer);
        System.out.println(customer.getLinkMans().size());//断点
    }
    transaction.commit();
    }

 

<set name="linkMans" cascade="save-update,delete" inverse="true" fetch="subselect" lazy="true" >

        执行到断点前结果:

Hibernate: 
    select
        this_.cust_id as cust_id1_0_0_,
        this_.cust_name as cust_nam2_0_0_,
        this_.cust_source as cust_sou3_0_0_,
        this_.cust_industry as cust_ind4_0_0_,
        this_.cust_level as cust_lev5_0_0_,
        this_.cust_phone as cust_pho6_0_0_,
        this_.cust_mobile as cust_mob7_0_0_ 
    from
        cst_customer this_
Customer [cust_id=1, cust_name=lisi, cust_source=1, cust_industry=null, cust_level=null, cust_phone=null, cust_mobile=null]

        执行到断点后结果:

Hibernate: 
    select
        linkmans0_.lkm_cust_id as lkm_cus10_1_1_,
        linkmans0_.lkm_id as lkm_id1_1_1_,
        linkmans0_.lkm_id as lkm_id1_1_0_,
        linkmans0_.lkm_name as lkm_name2_1_0_,
        linkmans0_.lkm_gender as lkm_gend3_1_0_,
        linkmans0_.lkm_phone as lkm_phon4_1_0_,
        linkmans0_.lkm_mobile as lkm_mobi5_1_0_,
        linkmans0_.lkm_email as lkm_emai6_1_0_,
        linkmans0_.lkm_qq as lkm_qq7_1_0_,
        linkmans0_.lkm_position as lkm_posi8_1_0_,
        linkmans0_.lkm_memo as lkm_memo9_1_0_,
        linkmans0_.lkm_cust_id as lkm_cus10_1_0_ 
    from
        cst_linkman linkmans0_ 
    where
        linkmans0_.lkm_cust_id in (
            select
                this_.cust_id 
            from
                cst_customer this_
        )
1

        2.2.2.6、 fetch="subselect" lazy="false"

<set name="linkMans" cascade="save-update,delete" inverse="true" fetch="subselect" lazy="false" >

        执行到断点前结果:

Hibernate: 
    select
        linkmans0_.lkm_cust_id as lkm_cus10_1_1_,
        linkmans0_.lkm_id as lkm_id1_1_1_,
        linkmans0_.lkm_id as lkm_id1_1_0_,
        linkmans0_.lkm_name as lkm_name2_1_0_,
        linkmans0_.lkm_gender as lkm_gend3_1_0_,
        linkmans0_.lkm_phone as lkm_phon4_1_0_,
        linkmans0_.lkm_mobile as lkm_mobi5_1_0_,
        linkmans0_.lkm_email as lkm_emai6_1_0_,
        linkmans0_.lkm_qq as lkm_qq7_1_0_,
        linkmans0_.lkm_position as lkm_posi8_1_0_,
        linkmans0_.lkm_memo as lkm_memo9_1_0_,
        linkmans0_.lkm_cust_id as lkm_cus10_1_0_ 
    from
        cst_linkman linkmans0_ 
    where
        linkmans0_.lkm_cust_id in (
            select
                this_.cust_id 
            from
                cst_customer this_
        )
Customer [cust_id=1, cust_name=lisi, cust_source=1, cust_industry=null, cust_level=null, cust_phone=null, cust_mobile=null]

        执行到断点后结果:1

        2.2.2.7、 fetch="subselect" lazy="extra"

<set name="linkMans" cascade="save-update,delete" inverse="true" fetch="subselect" lazy="extra" >

        执行到断点前结果:

Hibernate: 
    select
        this_.cust_id as cust_id1_0_0_,
        this_.cust_name as cust_nam2_0_0_,
        this_.cust_source as cust_sou3_0_0_,
        this_.cust_industry as cust_ind4_0_0_,
        this_.cust_level as cust_lev5_0_0_,
        this_.cust_phone as cust_pho6_0_0_,
        this_.cust_mobile as cust_mob7_0_0_ 
    from
        cst_customer this_
Customer [cust_id=1, cust_name=lisi, cust_source=1, cust_industry=null, cust_level=null, cust_phone=null, cust_mobile=null]

        执行到断点后结果:

Hibernate: 
    select
        count(lkm_id) 
    from
        cst_linkman 
    where
        lkm_cust_id =?
1

 

      2.2.3、<many-to-one>上的fetch和lazy

      • fetch :抓取策略,控制SQL语句格式。
        • select:默认值,发送普通的select语句,查询关联对象。
        • join:发送一条迫切左外连接。
      • lazy    :延迟加载,控制查询关联对象的时候是否采用延迟。
        • proxy :默认值,proxy具体的取值,取决于另一端的<class>上的lazy的值
        • false:查询关联对象,不采用延迟。
        • no-proxy:(不会使用)
      • 在实际开发中,一般都采用默认值。如果有特殊的需求,可能需要配置join。

        2.2.3.1、默认配置

<many-to-one name="customer" cascade="save-update" column="lkm_cust_id"
            class="com.hibernate.domain.Customer" />

          Customer.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <!-- 建立类与表的映射 -->
    <class name="com.hibernate.domain.Customer" table="cst_customer" >
        <!-- 建立类中的属性与表中的主键对应 -->
        <id name="cust_id" column="cust_id">
            <generator class="native"></generator>
        </id>

        <!-- 建立类中的普通属性与表中的字段对应 -->
        <property name="cust_name" column="cust_name"></property>
        <property name="cust_source" column="cust_source"></property>
        <property name="cust_industry" column="cust_industry"></property>
        <property name="cust_level" column="cust_level"></property>
        <property name="cust_phone" column="cust_phone"></property>
        <property name="cust_mobile" column="cust_mobile"></property>
        <!-- 配置一对多的映射:放置的多的一方的集合 -->
        <!-- set标签 :
         * name :多的一方的对象集合的属性名称。 
         * cascade:级联
         * inverse:放弃外键维护权。 -->
        <set name="linkMans" >
            <!--key标签 
            * column:多的一方的外键的名称。 -->
            <key column="lkm_cust_id" />
            <!-- one-to-many标签
             * class :多的一方的类的全路径 -->
            <one-to-many class="com.hibernate.domain.LinkMan" />
        </set>
    </class>
</hibernate-mapping>
    @Test
    public void demo5() {
    Session session = HibernateUtils.getCurrentSession();
    Transaction transaction = session.beginTransaction();

    LinkMan linkMan = session.get(LinkMan.class, 1l);
    System.out.println(linkMan.getCustomer().getCust_name());//此处设置断点
    transaction.commit();
    }

        执行到断点前结果:

Hibernate: 
    select
        linkman0_.lkm_id as lkm_id1_1_0_,
        linkman0_.lkm_name as lkm_name2_1_0_,
        linkman0_.lkm_gender as lkm_gend3_1_0_,
        linkman0_.lkm_phone as lkm_phon4_1_0_,
        linkman0_.lkm_mobile as lkm_mobi5_1_0_,
        linkman0_.lkm_email as lkm_emai6_1_0_,
        linkman0_.lkm_qq as lkm_qq7_1_0_,
        linkman0_.lkm_position as lkm_posi8_1_0_,
        linkman0_.lkm_memo as lkm_memo9_1_0_,
        linkman0_.lkm_cust_id as lkm_cus10_1_0_ 
    from
        cst_linkman linkman0_ 
    where
        linkman0_.lkm_id=?

        执行到断点后结果:

Hibernate: 
    select
        customer0_.cust_id as cust_id1_0_0_,
        customer0_.cust_name as cust_nam2_0_0_,
        customer0_.cust_source as cust_sou3_0_0_,
        customer0_.cust_industry as cust_ind4_0_0_,
        customer0_.cust_level as cust_lev5_0_0_,
        customer0_.cust_phone as cust_pho6_0_0_,
        customer0_.cust_mobile as cust_mob7_0_0_ 
    from
        cst_customer customer0_ 
    where
        customer0_.cust_id=?
lisi

        2.2.3.2、fetch="select" lazy="proxy"

<many-to-one name="customer" fetch="select" lazy="proxy" cascade="save-update" column="lkm_cust_id"
            class="com.hibernate.domain.Customer" />

        执行到断点前结果:

Hibernate: 
    select
        linkman0_.lkm_id as lkm_id1_1_0_,
        linkman0_.lkm_name as lkm_name2_1_0_,
        linkman0_.lkm_gender as lkm_gend3_1_0_,
        linkman0_.lkm_phone as lkm_phon4_1_0_,
        linkman0_.lkm_mobile as lkm_mobi5_1_0_,
        linkman0_.lkm_email as lkm_emai6_1_0_,
        linkman0_.lkm_qq as lkm_qq7_1_0_,
        linkman0_.lkm_position as lkm_posi8_1_0_,
        linkman0_.lkm_memo as lkm_memo9_1_0_,
        linkman0_.lkm_cust_id as lkm_cus10_1_0_ 
    from
        cst_linkman linkman0_ 
    where
        linkman0_.lkm_id=?

        执行到断点后结果:

Hibernate: 
    select
        customer0_.cust_id as cust_id1_0_0_,
        customer0_.cust_name as cust_nam2_0_0_,
        customer0_.cust_source as cust_sou3_0_0_,
        customer0_.cust_industry as cust_ind4_0_0_,
        customer0_.cust_level as cust_lev5_0_0_,
        customer0_.cust_phone as cust_pho6_0_0_,
        customer0_.cust_mobile as cust_mob7_0_0_ 
    from
        cst_customer customer0_ 
    where
        customer0_.cust_id=?
lisi

          2.2.3.2.1、fetch="select" lazy="proxy" 、关联对象的hbm.xml中<class>上的lazy=“false”

<class name="com.hibernate.domain.Customer" table="cst_customer" lazy="false" >

          执行到断点前结果:

Hibernate: 
    select
        linkman0_.lkm_id as lkm_id1_1_0_,
        linkman0_.lkm_name as lkm_name2_1_0_,
        linkman0_.lkm_gender as lkm_gend3_1_0_,
        linkman0_.lkm_phone as lkm_phon4_1_0_,
        linkman0_.lkm_mobile as lkm_mobi5_1_0_,
        linkman0_.lkm_email as lkm_emai6_1_0_,
        linkman0_.lkm_qq as lkm_qq7_1_0_,
        linkman0_.lkm_position as lkm_posi8_1_0_,
        linkman0_.lkm_memo as lkm_memo9_1_0_,
        linkman0_.lkm_cust_id as lkm_cus10_1_0_ 
    from
        cst_linkman linkman0_ 
    where
        linkman0_.lkm_id=?
Hibernate: 
    select
        customer0_.cust_id as cust_id1_0_0_,
        customer0_.cust_name as cust_nam2_0_0_,
        customer0_.cust_source as cust_sou3_0_0_,
        customer0_.cust_industry as cust_ind4_0_0_,
        customer0_.cust_level as cust_lev5_0_0_,
        customer0_.cust_phone as cust_pho6_0_0_,
        customer0_.cust_mobile as cust_mob7_0_0_ 
    from
        cst_customer customer0_ 
    where
        customer0_.cust_id=?

          执行到断点后结果:lisi

        2.2.3.3、fetch="select" lazy="false" 

<many-to-one name="customer" fetch="select" lazy="false" cascade="save-update" column="lkm_cust_id"
            class="com.hibernate.domain.Customer" />

        执行到断点前结果:

Hibernate: 
    select
        linkman0_.lkm_id as lkm_id1_1_0_,
        linkman0_.lkm_name as lkm_name2_1_0_,
        linkman0_.lkm_gender as lkm_gend3_1_0_,
        linkman0_.lkm_phone as lkm_phon4_1_0_,
        linkman0_.lkm_mobile as lkm_mobi5_1_0_,
        linkman0_.lkm_email as lkm_emai6_1_0_,
        linkman0_.lkm_qq as lkm_qq7_1_0_,
        linkman0_.lkm_position as lkm_posi8_1_0_,
        linkman0_.lkm_memo as lkm_memo9_1_0_,
        linkman0_.lkm_cust_id as lkm_cus10_1_0_ 
    from
        cst_linkman linkman0_ 
    where
        linkman0_.lkm_id=?
Hibernate: 
    select
        customer0_.cust_id as cust_id1_0_0_,
        customer0_.cust_name as cust_nam2_0_0_,
        customer0_.cust_source as cust_sou3_0_0_,
        customer0_.cust_industry as cust_ind4_0_0_,
        customer0_.cust_level as cust_lev5_0_0_,
        customer0_.cust_phone as cust_pho6_0_0_,
        customer0_.cust_mobile as cust_mob7_0_0_ 
    from
        cst_customer customer0_ 
    where
        customer0_.cust_id=?

        执行到断点后结果:lisi

        2.2.3.4、fetch="join" lazy=失效

<many-to-one name="customer" fetch="join" cascade="save-update" column="lkm_cust_id"
            class="com.hibernate.domain.Customer" />

        执行到断点前结果:

Hibernate: 
    select
        linkman0_.lkm_id as lkm_id1_1_0_,
        linkman0_.lkm_name as lkm_name2_1_0_,
        linkman0_.lkm_gender as lkm_gend3_1_0_,
        linkman0_.lkm_phone as lkm_phon4_1_0_,
        linkman0_.lkm_mobile as lkm_mobi5_1_0_,
        linkman0_.lkm_email as lkm_emai6_1_0_,
        linkman0_.lkm_qq as lkm_qq7_1_0_,
        linkman0_.lkm_position as lkm_posi8_1_0_,
        linkman0_.lkm_memo as lkm_memo9_1_0_,
        linkman0_.lkm_cust_id as lkm_cus10_1_0_,
        customer1_.cust_id as cust_id1_0_1_,
        customer1_.cust_name as cust_nam2_0_1_,
        customer1_.cust_source as cust_sou3_0_1_,
        customer1_.cust_industry as cust_ind4_0_1_,
        customer1_.cust_level as cust_lev5_0_1_,
        customer1_.cust_phone as cust_pho6_0_1_,
        customer1_.cust_mobile as cust_mob7_0_1_ 
    from
        cst_linkman linkman0_ 
    left outer join
        cst_customer customer1_ 
            on linkman0_.lkm_cust_id=customer1_.cust_id 
    where
        linkman0_.lkm_id=?

        执行到断点后结果:lisi

  ③、批量抓取(batch-size)

    通过查询语句一次查询多条记录,通过配置batch-size的值确定一次查询多少条记录(都是在一的一方的hbm.xml配置)

    1、通过客户获取联系人(需要配置set节点上batch-size,batch-size默认值为1)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <!-- 建立类与表的映射 -->
    <class name="com.hibernate.domain.Customer" table="cst_customer">
        <!-- 建立类中的属性与表中的主键对应 -->
        <id name="cust_id" column="cust_id">
            <generator class="native"></generator>
        </id>

        <!-- 建立类中的普通属性与表中的字段对应 -->
        <property name="cust_name" column="cust_name"></property>
        <property name="cust_source" column="cust_source"></property>
        <property name="cust_industry" column="cust_industry"></property>
        <property name="cust_level" column="cust_level"></property>
        <property name="cust_phone" column="cust_phone"></property>
        <property name="cust_mobile" column="cust_mobile"></property>
        <!-- 配置一对多的映射:放置的多的一方的集合 -->
        <!-- set标签 :
         * name :多的一方的对象集合的属性名称。 
         * cascade:级联
         * inverse:放弃外键维护权。 -->
        <set name="linkMans" >
            <!--key标签 
            * column:多的一方的外键的名称。 -->
            <key column="lkm_cust_id" />
            <!-- one-to-many标签
             * class :多的一方的类的全路径 -->
            <one-to-many class="com.hibernate.domain.LinkMan" />
        </set>
    </class>
</hibernate-mapping>
    @Test
    public void demo5() {
    Session session = HibernateUtils.getCurrentSession();
    Transaction transaction = session.beginTransaction();

    List<Customer> list = session.createQuery("from Customer").list();
    for (Customer customer : list) {
        System.out.println(customer.getCust_name());
        for (LinkMan linkMan : customer.getLinkMans()) { //设置断点
        System.out.println(linkMan.getLkm_name());
        }
    }
    transaction.commit();
    }

    执行到断点前结果:

Hibernate: 
    select
        customer0_.cust_id as cust_id1_0_,
        customer0_.cust_name as cust_nam2_0_,
        customer0_.cust_source as cust_sou3_0_,
        customer0_.cust_industry as cust_ind4_0_,
        customer0_.cust_level as cust_lev5_0_,
        customer0_.cust_phone as cust_pho6_0_,
        customer0_.cust_mobile as cust_mob7_0_ 
    from
        cst_customer customer0_
lisi

    执行到断点后结果:

Hibernate: 
    select
        linkmans0_.lkm_cust_id as lkm_cus10_1_0_,
        linkmans0_.lkm_id as lkm_id1_1_0_,
        linkmans0_.lkm_id as lkm_id1_1_1_,
        linkmans0_.lkm_name as lkm_name2_1_1_,
        linkmans0_.lkm_gender as lkm_gend3_1_1_,
        linkmans0_.lkm_phone as lkm_phon4_1_1_,
        linkmans0_.lkm_mobile as lkm_mobi5_1_1_,
        linkmans0_.lkm_email as lkm_emai6_1_1_,
        linkmans0_.lkm_qq as lkm_qq7_1_1_,
        linkmans0_.lkm_position as lkm_posi8_1_1_,
        linkmans0_.lkm_memo as lkm_memo9_1_1_,
        linkmans0_.lkm_cust_id as lkm_cus10_1_1_ 
    from
        cst_linkman linkmans0_ 
    where
        linkmans0_.lkm_cust_id=?

Hibernate: 
    select
        linkmans0_.lkm_cust_id as lkm_cus10_1_0_,
        linkmans0_.lkm_id as lkm_id1_1_0_,
        linkmans0_.lkm_id as lkm_id1_1_1_,
        linkmans0_.lkm_name as lkm_name2_1_1_,
        linkmans0_.lkm_gender as lkm_gend3_1_1_,
        linkmans0_.lkm_phone as lkm_phon4_1_1_,
        linkmans0_.lkm_mobile as lkm_mobi5_1_1_,
        linkmans0_.lkm_email as lkm_emai6_1_1_,
        linkmans0_.lkm_qq as lkm_qq7_1_1_,
        linkmans0_.lkm_position as lkm_posi8_1_1_,
        linkmans0_.lkm_memo as lkm_memo9_1_1_,
        linkmans0_.lkm_cust_id as lkm_cus10_1_1_ 
    from
        cst_linkman linkmans0_ 
    where
        linkmans0_.lkm_cust_id=?

Hibernate: 
    select
        linkmans0_.lkm_cust_id as lkm_cus10_1_0_,
        linkmans0_.lkm_id as lkm_id1_1_0_,
        linkmans0_.lkm_id as lkm_id1_1_1_,
        linkmans0_.lkm_name as lkm_name2_1_1_,
        linkmans0_.lkm_gender as lkm_gend3_1_1_,
        linkmans0_.lkm_phone as lkm_phon4_1_1_,
        linkmans0_.lkm_mobile as lkm_mobi5_1_1_,
        linkmans0_.lkm_email as lkm_emai6_1_1_,
        linkmans0_.lkm_qq as lkm_qq7_1_1_,
        linkmans0_.lkm_position as lkm_posi8_1_1_,
        linkmans0_.lkm_memo as lkm_memo9_1_1_,
        linkmans0_.lkm_cust_id as lkm_cus10_1_1_ 
    from
        cst_linkman linkmans0_ 
    where
        linkmans0_.lkm_cust_id=?

Hibernate: 
    select
        linkmans0_.lkm_cust_id as lkm_cus10_1_0_,
        linkmans0_.lkm_id as lkm_id1_1_0_,
        linkmans0_.lkm_id as lkm_id1_1_1_,
        linkmans0_.lkm_name as lkm_name2_1_1_,
        linkmans0_.lkm_gender as lkm_gend3_1_1_,
        linkmans0_.lkm_phone as lkm_phon4_1_1_,
        linkmans0_.lkm_mobile as lkm_mobi5_1_1_,
        linkmans0_.lkm_email as lkm_emai6_1_1_,
        linkmans0_.lkm_qq as lkm_qq7_1_1_,
        linkmans0_.lkm_position as lkm_posi8_1_1_,
        linkmans0_.lkm_memo as lkm_memo9_1_1_,
        linkmans0_.lkm_cust_id as lkm_cus10_1_1_ 
    from
        cst_linkman linkmans0_ 
    where
        linkmans0_.lkm_cust_id=?

Hibernate: 
    select
        linkmans0_.lkm_cust_id as lkm_cus10_1_0_,
        linkmans0_.lkm_id as lkm_id1_1_0_,
        linkmans0_.lkm_id as lkm_id1_1_1_,
        linkmans0_.lkm_name as lkm_name2_1_1_,
        linkmans0_.lkm_gender as lkm_gend3_1_1_,
        linkmans0_.lkm_phone as lkm_phon4_1_1_,
        linkmans0_.lkm_mobile as lkm_mobi5_1_1_,
        linkmans0_.lkm_email as lkm_emai6_1_1_,
        linkmans0_.lkm_qq as lkm_qq7_1_1_,
        linkmans0_.lkm_position as lkm_posi8_1_1_,
        linkmans0_.lkm_memo as lkm_memo9_1_1_,
        linkmans0_.lkm_cust_id as lkm_cus10_1_1_ 
    from
        cst_linkman linkmans0_ 
    where
        linkmans0_.lkm_cust_id=?

Hibernate: 
    select
        linkmans0_.lkm_cust_id as lkm_cus10_1_0_,
        linkmans0_.lkm_id as lkm_id1_1_0_,
        linkmans0_.lkm_id as lkm_id1_1_1_,
        linkmans0_.lkm_name as lkm_name2_1_1_,
        linkmans0_.lkm_gender as lkm_gend3_1_1_,
        linkmans0_.lkm_phone as lkm_phon4_1_1_,
        linkmans0_.lkm_mobile as lkm_mobi5_1_1_,
        linkmans0_.lkm_email as lkm_emai6_1_1_,
        linkmans0_.lkm_qq as lkm_qq7_1_1_,
        linkmans0_.lkm_position as lkm_posi8_1_1_,
        linkmans0_.lkm_memo as lkm_memo9_1_1_,
        linkmans0_.lkm_cust_id as lkm_cus10_1_1_ 
    from
        cst_linkman linkmans0_ 
    where
        linkmans0_.lkm_cust_id=?

    2、通过客户获取联系人(需要配置set节点上batch-size=“3”)

<set name="linkMans" batch-size="3" >

    执行到断点前结果:

Hibernate: 
    select
        customer0_.cust_id as cust_id1_0_,
        customer0_.cust_name as cust_nam2_0_,
        customer0_.cust_source as cust_sou3_0_,
        customer0_.cust_industry as cust_ind4_0_,
        customer0_.cust_level as cust_lev5_0_,
        customer0_.cust_phone as cust_pho6_0_,
        customer0_.cust_mobile as cust_mob7_0_ 
    from
        cst_customer customer0_
lisi

    执行到断点后结果:

Hibernate: 
    select
        linkmans0_.lkm_cust_id as lkm_cus10_1_1_,
        linkmans0_.lkm_id as lkm_id1_1_1_,
        linkmans0_.lkm_id as lkm_id1_1_0_,
        linkmans0_.lkm_name as lkm_name2_1_0_,
        linkmans0_.lkm_gender as lkm_gend3_1_0_,
        linkmans0_.lkm_phone as lkm_phon4_1_0_,
        linkmans0_.lkm_mobile as lkm_mobi5_1_0_,
        linkmans0_.lkm_email as lkm_emai6_1_0_,
        linkmans0_.lkm_qq as lkm_qq7_1_0_,
        linkmans0_.lkm_position as lkm_posi8_1_0_,
        linkmans0_.lkm_memo as lkm_memo9_1_0_,
        linkmans0_.lkm_cust_id as lkm_cus10_1_0_ 
    from
        cst_linkman linkmans0_ 
    where
        linkmans0_.lkm_cust_id in (
            ?, ?, ?
        )

Hibernate: 
    select
        linkmans0_.lkm_cust_id as lkm_cus10_1_1_,
        linkmans0_.lkm_id as lkm_id1_1_1_,
        linkmans0_.lkm_id as lkm_id1_1_0_,
        linkmans0_.lkm_name as lkm_name2_1_0_,
        linkmans0_.lkm_gender as lkm_gend3_1_0_,
        linkmans0_.lkm_phone as lkm_phon4_1_0_,
        linkmans0_.lkm_mobile as lkm_mobi5_1_0_,
        linkmans0_.lkm_email as lkm_emai6_1_0_,
        linkmans0_.lkm_qq as lkm_qq7_1_0_,
        linkmans0_.lkm_position as lkm_posi8_1_0_,
        linkmans0_.lkm_memo as lkm_memo9_1_0_,
        linkmans0_.lkm_cust_id as lkm_cus10_1_0_ 
    from
        cst_linkman linkmans0_ 
    where
        linkmans0_.lkm_cust_id in (
            ?, ?, ?
        )

    配置batch-size后,每次查询条件个数为batch-size值,如果查询条件个数小于batch-size的值,那就查询一次,否则就会查询batch-size值的倍数次

    3、通过联系人查找客户

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <!-- 建立类与表的映射 -->
    <class name="com.hibernate.domain.Customer" table="cst_customer">
        <!-- 建立类中的属性与表中的主键对应 -->
        <id name="cust_id" column="cust_id">
            <generator class="native"></generator>
        </id>

        <!-- 建立类中的普通属性与表中的字段对应 -->
        <property name="cust_name" column="cust_name"></property>
        <property name="cust_source" column="cust_source"></property>
        <property name="cust_industry" column="cust_industry"></property>
        <property name="cust_level" column="cust_level"></property>
        <property name="cust_phone" column="cust_phone"></property>
        <property name="cust_mobile" column="cust_mobile"></property>
        <!-- 配置一对多的映射:放置的多的一方的集合 -->
        <!-- set标签 :
         * name :多的一方的对象集合的属性名称。 
         * cascade:级联
         * inverse:放弃外键维护权。 -->
        <set name="linkMans" >
            <!--key标签 
            * column:多的一方的外键的名称。 -->
            <key column="lkm_cust_id" />
            <!-- one-to-many标签
             * class :多的一方的类的全路径 -->
            <one-to-many class="com.hibernate.domain.LinkMan" />
        </set>
    </class>
</hibernate-mapping>

    结果:Customer被查询多次

Hibernate: 
    select
        linkman0_.lkm_id as lkm_id1_1_,
        linkman0_.lkm_name as lkm_name2_1_,
        linkman0_.lkm_gender as lkm_gend3_1_,
        linkman0_.lkm_phone as lkm_phon4_1_,
        linkman0_.lkm_mobile as lkm_mobi5_1_,
        linkman0_.lkm_email as lkm_emai6_1_,
        linkman0_.lkm_qq as lkm_qq7_1_,
        linkman0_.lkm_position as lkm_posi8_1_,
        linkman0_.lkm_memo as lkm_memo9_1_,
        linkman0_.lkm_cust_id as lkm_cus10_1_ 
    from
        cst_linkman linkman0_

Hibernate: 
    select
        customer0_.cust_id as cust_id1_0_0_,
        customer0_.cust_name as cust_nam2_0_0_,
        customer0_.cust_source as cust_sou3_0_0_,
        customer0_.cust_industry as cust_ind4_0_0_,
        customer0_.cust_level as cust_lev5_0_0_,
        customer0_.cust_phone as cust_pho6_0_0_,
        customer0_.cust_mobile as cust_mob7_0_0_ 
    from
        cst_customer customer0_ 
    where
        customer0_.cust_id=?

Hibernate: 
    select
        customer0_.cust_id as cust_id1_0_0_,
        customer0_.cust_name as cust_nam2_0_0_,
        customer0_.cust_source as cust_sou3_0_0_,
        customer0_.cust_industry as cust_ind4_0_0_,
        customer0_.cust_level as cust_lev5_0_0_,
        customer0_.cust_phone as cust_pho6_0_0_,
        customer0_.cust_mobile as cust_mob7_0_0_ 
    from
        cst_customer customer0_ 
    where
        customer0_.cust_id=?

Hibernate: 
    select
        customer0_.cust_id as cust_id1_0_0_,
        customer0_.cust_name as cust_nam2_0_0_,
        customer0_.cust_source as cust_sou3_0_0_,
        customer0_.cust_industry as cust_ind4_0_0_,
        customer0_.cust_level as cust_lev5_0_0_,
        customer0_.cust_phone as cust_pho6_0_0_,
        customer0_.cust_mobile as cust_mob7_0_0_ 
    from
        cst_customer customer0_ 
    where
        customer0_.cust_id=?

Hibernate: 
    select
        customer0_.cust_id as cust_id1_0_0_,
        customer0_.cust_name as cust_nam2_0_0_,
        customer0_.cust_source as cust_sou3_0_0_,
        customer0_.cust_industry as cust_ind4_0_0_,
        customer0_.cust_level as cust_lev5_0_0_,
        customer0_.cust_phone as cust_pho6_0_0_,
        customer0_.cust_mobile as cust_mob7_0_0_ 
    from
        cst_customer customer0_ 
    where
        customer0_.cust_id=?

Hibernate: 
    select
        customer0_.cust_id as cust_id1_0_0_,
        customer0_.cust_name as cust_nam2_0_0_,
        customer0_.cust_source as cust_sou3_0_0_,
        customer0_.cust_industry as cust_ind4_0_0_,
        customer0_.cust_level as cust_lev5_0_0_,
        customer0_.cust_phone as cust_pho6_0_0_,
        customer0_.cust_mobile as cust_mob7_0_0_ 
    from
        cst_customer customer0_ 
    where
        customer0_.cust_id=?

Hibernate: 
    select
        customer0_.cust_id as cust_id1_0_0_,
        customer0_.cust_name as cust_nam2_0_0_,
        customer0_.cust_source as cust_sou3_0_0_,
        customer0_.cust_industry as cust_ind4_0_0_,
        customer0_.cust_level as cust_lev5_0_0_,
        customer0_.cust_phone as cust_pho6_0_0_,
        customer0_.cust_mobile as cust_mob7_0_0_ 
    from
        cst_customer customer0_ 
    where
        customer0_.cust_id=?

        3.1、修改<class> 节点上batch-size属性值

<class name="com.hibernate.domain.Customer" table="cst_customer" batch-size="6">

        结果:Customer被查询一次

Hibernate: 
    select
        linkman0_.lkm_id as lkm_id1_1_,
        linkman0_.lkm_name as lkm_name2_1_,
        linkman0_.lkm_gender as lkm_gend3_1_,
        linkman0_.lkm_phone as lkm_phon4_1_,
        linkman0_.lkm_mobile as lkm_mobi5_1_,
        linkman0_.lkm_email as lkm_emai6_1_,
        linkman0_.lkm_qq as lkm_qq7_1_,
        linkman0_.lkm_position as lkm_posi8_1_,
        linkman0_.lkm_memo as lkm_memo9_1_,
        linkman0_.lkm_cust_id as lkm_cus10_1_ 
    from
        cst_linkman linkman0_

Hibernate: 
    select
        customer0_.cust_id as cust_id1_0_0_,
        customer0_.cust_name as cust_nam2_0_0_,
        customer0_.cust_source as cust_sou3_0_0_,
        customer0_.cust_industry as cust_ind4_0_0_,
        customer0_.cust_level as cust_lev5_0_0_,
        customer0_.cust_phone as cust_pho6_0_0_,
        customer0_.cust_mobile as cust_mob7_0_0_ 
    from
        cst_customer customer0_ 
    where
        customer0_.cust_id in (
            ?, ?, ?, ?, ?, ?
        )

 

posted @ 2020-11-12 16:50  一杯水M  阅读(102)  评论(0编辑  收藏  举报