Hibernate延迟加载策略

 

  所谓懒加载(lazy)就是延时加载,就是当在真正需要数据的时候,才真正执行数据加载操作

  至于为什么要用懒加载呢,就是当我们要访问的数据量过大时,明显用缓存不太合适,因为内存容量有限 ,为了减少并发量,减少系统资源的消耗,我们让数据在需要的时候才进行加载,这时我们就用到了懒加载。

 

1.类级别的懒加载(session.load()方法)

  get方法:没有任何策略.调用即立即查询数据库加载数据.

  load方法: 应用类级别的加载策略

1.get方法测试,没有任何延迟加载策略

    @Test
    // get方法 : 立即加载.执行方法时立即发送sql语句查询结果
    public void fun1(){
        Session session = HibernateUtils.openSession();
        Transaction tx = session.beginTransaction();
        //----------------------------------------------------
        
        Customer customer = session.get(Customer.class, 1l);//执行完此句即查询发出SQL
        
        System.out.println(customer);
        //----------------------------------------------------
        tx.commit();
        session.close();
        
    }

 

 没有延迟加载的get方法执行时候就发出SQL请求,且生成的对象是原生的JavaBean对象。

  

 

 

 2.load方法测试,测试懒加载策略

 修改配置文件懒加载设为true,默认就是开启懒加载的.比如我们查询customer的时候不会查询linkman,而我们调用get方法获取linkman的时候才去获取linkman。

     lazy(默认值):true, 查询类时,会返回代理对象.会在使用属性时,根据关联的session查询数据库.加载数据.

     lazy:false. load方法会与get方法没有任何区别.调用时即加载数据.

    结论:为了提高效率.建议使用延迟加载(懒加载)

 

原理:

   延迟加载是生成javaassist代理对象,生成代理对象的作用是依赖于session在使用对象的时候利用session去查询数据库,所以在使用懒加载时要确保,调用属性加载数据时,session还是打开的.不然会抛出异常

 

 

测试代码:

    @Test
    // load方法(默认):是在执行时,不发送任何sql语句.返回一个对象.使用该对象时,才执行查询.
    // 延迟加载: 仅仅获得没有使用.不会查询.在使用时才进行查询.
    // 是否对类进行延迟加载: 可以通过在class元素上配置lazy属性来控制.
        //lazy:true  加载时,不查询.使用时才查询
        //lazy:false 加载时立即查询.
    public void fun2(){
        Session session = HibernateUtils.openSession();
        Transaction tx = session.beginTransaction();
        //----------------------------------------------------
        
        Customer customer = session.load(Customer.class, 1l);
        
        System.out.println(customer);
        //----------------------------------------------------
        tx.commit();
        session.close();
    }

 

查看生成的代理对象:(代理对象的名称后面带有$符号)

 

 

 注意:我们在使用懒加载的时候不能再关闭session之后再使用对象,因为懒加载的代理对象依赖session再使用对象的时候查询数据库,如下面的代码是错误的:

    public void fun2(){
        Session session = HibernateUtils.openSession();
        Transaction tx = session.beginTransaction();
        //----------------------------------------------------
        
        Customer customer = session.load(Customer.class, 1l);
        
        //----------------------------------------------------
        tx.commit();
        session.close();
        System.out.println(customer);
    }

 

结果会报错:

 

 

2.关联级别的查询(fetch、lazy、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 package="cn.qlq.domain" >
    <class name="Customer" table="cst_customer" >
        <id name="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_linkman" column="cust_linkman" ></property>
        <property name="cust_phone" column="cust_phone" ></property>
        <property name="cust_mobile" column="cust_mobile" ></property>
    
    <!-- 
        lazy属性: 决定是否延迟加载
            true(默认值): 延迟加载,懒加载
            false: 立即加载
            extra: 极其懒惰
        fetch属性: 决定加载策略.使用什么类型的sql语句加载集合数据
            select(默认值): 单表查询加载
            join: 使用多表查询加载集合
            subselect:使用子查询加载集合
     -->
     <!-- batch-size: 抓取集合的数量为3.
             抓取客户的集合时,一次抓取几个客户的联系人集合.
      -->
        <set name="linkMens">
            <key column="lkm_cust_id" ></key>
            <one-to-many class="LinkMan" />
        </set>
    </class>
</hibernate-mapping>

 

 

(1)fetch为select的测试:

  • lazy=true的测试
    // 集合级别的关联
    // fetch:select 单表查询
    // lazy:true 使用时才加载集合数据.
    @Test
    public void fun1() {
        Session session = HibernateUtil.openSession();
        Transaction tx = session.beginTransaction();
        // ----------------------------------------------------

        Customer c = session.get(Customer.class, 7l);

        Set<LinkMan> linkMens = c.getLinkMens();// 关联级别

        System.out.println(linkMens);//打印SQL

        // ----------------------------------------------------
        tx.commit();
        session.close();

    }

 

SQL:

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_linkman as cust_lin6_0_0_,
        customer0_.cust_phone as cust_pho7_0_0_,
        customer0_.cust_mobile as cust_mob8_0_0_ 
    from
        cst_customer customer0_ 
    where
        customer0_.cust_id=?
Hibernate: 
    select
        linkmens0_.lkm_cust_id as lkm_cus10_1_0_,
        linkmens0_.lkm_id as lkm_id1_1_0_,
        linkmens0_.lkm_id as lkm_id1_1_1_,
        linkmens0_.lkm_gender as lkm_gend2_1_1_,
        linkmens0_.lkm_name as lkm_name3_1_1_,
        linkmens0_.lkm_phone as lkm_phon4_1_1_,
        linkmens0_.lkm_email as lkm_emai5_1_1_,
        linkmens0_.lkm_qq as lkm_qq6_1_1_,
        linkmens0_.lkm_mobile as lkm_mobi7_1_1_,
        linkmens0_.lkm_memo as lkm_memo8_1_1_,
        linkmens0_.lkm_position as lkm_posi9_1_1_,
        linkmens0_.lkm_cust_id as lkm_cus10_1_1_ 
    from
        cst_linkman linkmens0_ 
    where
        linkmens0_.lkm_cust_id=?
[LinkMan [lkm_id=3, lkm_gender=null, lkm_name=王八, lkm_phone=null, lkm_email=null, lkm_qq=null, lkm_mobile=null, lkm_memo=null, lkm_position=null, customer=Customer [cust_id=7, cust_name=测试名称1]], LinkMan [lkm_id=4, lkm_gender=null, lkm_name=田七, lkm_phone=null, lkm_email=null, lkm_qq=null, lkm_mobile=null, lkm_memo=null, lkm_position=null, customer=Customer [cust_id=7, cust_name=测试名称1]]]

 

 

  • lazy=false的测试(会立即加载)
    // 集合级别的关联
    // fetch:select 单表查询
    // lazy:false 立即记载集合数据
    @Test
    public void fun2() {
        Session session = HibernateUtil.openSession();
        Transaction tx = session.beginTransaction();
        // ----------------------------------------------------

        Customer c = session.get(Customer.class, 7l);//加载所有SQL,包括查询linkman

        Set<LinkMan> linkMens = c.getLinkMens();// 关联级别

        System.out.println(linkMens);

        // ----------------------------------------------------
        tx.commit();
        session.close();

    }

 

 

  • lazy=xetra极其懒惰,与懒加载效果基本一致. 如果只获得集合的size.只查询集合的size(count语句)
    // 集合级别的关联
    // fetch:select 单表查询
    // lazy:extra 极其懒惰.与懒加载效果基本一致. 如果只获得集合的size.只查询集合的size(count语句)
    @Test
    public void fun3() {
        Session session = HibernateUtil.openSession();
        Transaction tx = session.beginTransaction();
        // ----------------------------------------------------

        Customer c = session.get(Customer.class, 7l);

        Set<LinkMan> linkMens = c.getLinkMens();// 关联级别

        System.out.println(linkMens.size());//count   SQL

        System.out.println(linkMens);//查询SQL

        // ----------------------------------------------------
        tx.commit();
        session.close();

    }

 

SQL:

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_linkman as cust_lin6_0_0_,
        customer0_.cust_phone as cust_pho7_0_0_,
        customer0_.cust_mobile as cust_mob8_0_0_ 
    from
        cst_customer customer0_ 
    where
        customer0_.cust_id=?
Hibernate: 
    select
        count(lkm_id) 
    from
        cst_linkman 
    where
        lkm_cust_id =?
2
Hibernate: 
    select
        linkmens0_.lkm_cust_id as lkm_cus10_1_0_,
        linkmens0_.lkm_id as lkm_id1_1_0_,
        linkmens0_.lkm_id as lkm_id1_1_1_,
        linkmens0_.lkm_gender as lkm_gend2_1_1_,
        linkmens0_.lkm_name as lkm_name3_1_1_,
        linkmens0_.lkm_phone as lkm_phon4_1_1_,
        linkmens0_.lkm_email as lkm_emai5_1_1_,
        linkmens0_.lkm_qq as lkm_qq6_1_1_,
        linkmens0_.lkm_mobile as lkm_mobi7_1_1_,
        linkmens0_.lkm_memo as lkm_memo8_1_1_,
        linkmens0_.lkm_position as lkm_posi9_1_1_,
        linkmens0_.lkm_cust_id as lkm_cus10_1_1_ 
    from
        cst_linkman linkmens0_ 
    where
        linkmens0_.lkm_cust_id=?

 

 

(2)fetch为join的多表查询: 延迟策略失效

    // 集合级别的关联
    // fetch:join 多表查询
    // lazy:true|false|extra 失效.立即加载.
    @Test
    public void fun4() {
        Session session = HibernateUtil.openSession();
        Transaction tx = session.beginTransaction();
        // ----------------------------------------------------

        Customer c = session.get(Customer.class, 7l);

        Set<LinkMan> linkMens = c.getLinkMens();// 关联级别

        System.out.println(linkMens.size());

        System.out.println(linkMens);

        // ----------------------------------------------------
        tx.commit();
        session.close();

    }

 

SQL:

    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_linkman as cust_lin6_0_0_,
        customer0_.cust_phone as cust_pho7_0_0_,
        customer0_.cust_mobile as cust_mob8_0_0_,
        linkmens1_.lkm_cust_id as lkm_cus10_1_1_,
        linkmens1_.lkm_id as lkm_id1_1_1_,
        linkmens1_.lkm_id as lkm_id1_1_2_,
        linkmens1_.lkm_gender as lkm_gend2_1_2_,
        linkmens1_.lkm_name as lkm_name3_1_2_,
        linkmens1_.lkm_phone as lkm_phon4_1_2_,
        linkmens1_.lkm_email as lkm_emai5_1_2_,
        linkmens1_.lkm_qq as lkm_qq6_1_2_,
        linkmens1_.lkm_mobile as lkm_mobi7_1_2_,
        linkmens1_.lkm_memo as lkm_memo8_1_2_,
        linkmens1_.lkm_position as lkm_posi9_1_2_,
        linkmens1_.lkm_cust_id as lkm_cus10_1_2_ 
    from
        cst_customer customer0_ 
    left outer join
        cst_linkman linkmens1_ 
            on customer0_.cust_id=linkmens1_.lkm_cust_id 
    where
        customer0_.cust_id=?

 

(3)fetch为subselect (只有在批量查询的时候才有用,如果是查询单个的话与select的作用一样)

  • lazy为true
    @Test
    // fetch: subselect 子查询
    // lazy: true 懒加载
    public void fun5() {
        Session session = HibernateUtil.openSession();
        Transaction tx = session.beginTransaction();
        // ----------------------------------------------------

        String hql = "from Customer";

        Query query = session.createQuery(hql);

        List<Customer> list = query.list();

        for (Customer c : list) {
            System.out.println(c);
            System.out.println(c.getLinkMens().size());
            System.out.println(c.getLinkMens());
        }

        // ----------------------------------------------------
        tx.commit();
        session.close();

    }

 

SQL:

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_linkman as cust_lin6_0_,
        customer0_.cust_phone as cust_pho7_0_,
        customer0_.cust_mobile as cust_mob8_0_ 
    from
        cst_customer customer0_
Customer [cust_id=7, cust_name=测试名称1]
Hibernate: 
    select
        linkmens0_.lkm_cust_id as lkm_cus10_1_1_,
        linkmens0_.lkm_id as lkm_id1_1_1_,
        linkmens0_.lkm_id as lkm_id1_1_0_,
        linkmens0_.lkm_gender as lkm_gend2_1_0_,
        linkmens0_.lkm_name as lkm_name3_1_0_,
        linkmens0_.lkm_phone as lkm_phon4_1_0_,
        linkmens0_.lkm_email as lkm_emai5_1_0_,
        linkmens0_.lkm_qq as lkm_qq6_1_0_,
        linkmens0_.lkm_mobile as lkm_mobi7_1_0_,
        linkmens0_.lkm_memo as lkm_memo8_1_0_,
        linkmens0_.lkm_position as lkm_posi9_1_0_,
        linkmens0_.lkm_cust_id as lkm_cus10_1_0_ 
    from
        cst_linkman linkmens0_ 
    where
        linkmens0_.lkm_cust_id in (
            select
                customer0_.cust_id 
            from
                cst_customer customer0_
        )

 

  • lazy为false,立即加载
    @Test
    // fetch: subselect 子查询
    // lazy: false 立即加载
    public void fun6() {
        Session session = HibernateUtil.openSession();
        Transaction tx = session.beginTransaction();
        // ----------------------------------------------------

        String hql = "from Customer";

        Query query = session.createQuery(hql);

        List<Customer> list = query.list();

        for (Customer c : list) {
            System.out.println(c);
            System.out.println(c.getLinkMens().size());
            System.out.println(c.getLinkMens());
        }

        // ----------------------------------------------------
        tx.commit();
        session.close();

    }

 

 

  • lazy为extra
    @Test
    // fetch: subselect 子查询
    // lazy: extra 极其懒惰
    public void fun7() {
        Session session = HibernateUtil.openSession();
        Transaction tx = session.beginTransaction();
        // ----------------------------------------------------

        String hql = "from Customer";

        Query query = session.createQuery(hql);

        List<Customer> list = query.list();

        for (Customer c : list) {
            System.out.println(c);
            System.out.println(c.getLinkMens().size());
            System.out.println(c.getLinkMens());
        }

        // ----------------------------------------------------
        tx.commit();
        session.close();

    }

 

SQL:

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_linkman as cust_lin6_0_,
        customer0_.cust_phone as cust_pho7_0_,
        customer0_.cust_mobile as cust_mob8_0_ 
    from
        cst_customer customer0_
Customer [cust_id=7, cust_name=测试名称1]
Hibernate: 
    select
        linkmens0_.lkm_cust_id as lkm_cus10_1_1_,
        linkmens0_.lkm_id as lkm_id1_1_1_,
        linkmens0_.lkm_id as lkm_id1_1_0_,
        linkmens0_.lkm_gender as lkm_gend2_1_0_,
        linkmens0_.lkm_name as lkm_name3_1_0_,
        linkmens0_.lkm_phone as lkm_phon4_1_0_,
        linkmens0_.lkm_email as lkm_emai5_1_0_,
        linkmens0_.lkm_qq as lkm_qq6_1_0_,
        linkmens0_.lkm_mobile as lkm_mobi7_1_0_,
        linkmens0_.lkm_memo as lkm_memo8_1_0_,
        linkmens0_.lkm_position as lkm_posi9_1_0_,
        linkmens0_.lkm_cust_id as lkm_cus10_1_0_ 
    from
        cst_linkman linkmens0_ 
    where
        linkmens0_.lkm_cust_id in (
            select
                customer0_.cust_id 
            from
                cst_customer customer0_
        )

 

2.关联属性懒加载(先查询联系人,跟据联系人反查顾客)

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 package="cn.qlq.domain" >
    <class name="LinkMan" table="cst_linkman" >
        <id name="lkm_id"  >
            <generator class="native"></generator>
        </id>
        <property name="lkm_gender"  ></property>
        <property name="lkm_name"  ></property>
        <property name="lkm_phone"  ></property>
        <property name="lkm_email"  ></property>
        <property name="lkm_qq"  ></property>
        <property name="lkm_mobile"  ></property>
        <property name="lkm_memo"  ></property>
        <property name="lkm_position"  ></property>
        <!-- 
        fetch 决定加载的sql语句
            select: 使用单表查询
            join : 多表查询 (会导致lazy失效)
        lazy  决定加载时机
            false: 立即加载
            proxy: 由customer的类级别加载策略决定.
         -->
        <many-to-one name="customer" column="lkm_cust_id" class="Customer" fetch="select" lazy="proxy"></many-to-one>
    </class>
</hibernate-mapping>

 

 

1.fetch为select,lazy为proxy,Customer类的lazy为true

 

测试代码:

    // 集合级别的关联
    // fetch:select 单表查询
    // lazy:proxy 代理,也就是由Customer的lazy属性决定是否懒加载
        //Customer的lazy为true
    @Test
    public void fun1() {
        Session session = HibernateUtil.openSession();
        Transaction tx = session.beginTransaction();
        // ----------------------------------------------------

        LinkMan linkMan = session.get(LinkMan.class, 5l);
        System.out.println("-------1------------");
        Customer customer = linkMan.getCustomer();
        System.out.println("-------2------------");
        System.out.println(customer);// 打印SQL

        // ----------------------------------------------------
        tx.commit();
        session.close();

    }

 

SQL:

Hibernate: 
    select
        linkman0_.lkm_id as lkm_id1_1_0_,
        linkman0_.lkm_gender as lkm_gend2_1_0_,
        linkman0_.lkm_name as lkm_name3_1_0_,
        linkman0_.lkm_phone as lkm_phon4_1_0_,
        linkman0_.lkm_email as lkm_emai5_1_0_,
        linkman0_.lkm_qq as lkm_qq6_1_0_,
        linkman0_.lkm_mobile as lkm_mobi7_1_0_,
        linkman0_.lkm_memo as lkm_memo8_1_0_,
        linkman0_.lkm_position as lkm_posi9_1_0_,
        linkman0_.lkm_cust_id as lkm_cus10_1_0_ 
    from
        cst_linkman linkman0_ 
    where
        linkman0_.lkm_id=?
-------1------------
-------2------------
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_linkman as cust_lin6_0_0_,
        customer0_.cust_phone as cust_pho7_0_0_,
        customer0_.cust_mobile as cust_mob8_0_0_ 
    from
        cst_customer customer0_ 
    where
        customer0_.cust_id=?
Customer [cust_id=9, cust_name=测试名称3]

 

 

2.fetch为select,lazy为proxy,Customer类的lazy为false

 

测试代码:

    // 集合级别的关联
    // fetch:select 单表查询
    // lazy:proxy 代理,也就是由Customer的lazy属性决定是否懒加载
    //Customer的lazy为false
    @Test
    public void fun2() {
        Session session = HibernateUtil.openSession();
        Transaction tx = session.beginTransaction();
        // ----------------------------------------------------
        
        LinkMan linkMan = session.get(LinkMan.class, 5l);
        System.out.println("-------1------------");
        Customer customer = linkMan.getCustomer();
        System.out.println("-------2------------");
        System.out.println(customer);// 打印SQL
        
        // ----------------------------------------------------
        tx.commit();
        session.close();
        
    }

 

SQL:

Hibernate: 
    select
        linkman0_.lkm_id as lkm_id1_1_0_,
        linkman0_.lkm_gender as lkm_gend2_1_0_,
        linkman0_.lkm_name as lkm_name3_1_0_,
        linkman0_.lkm_phone as lkm_phon4_1_0_,
        linkman0_.lkm_email as lkm_emai5_1_0_,
        linkman0_.lkm_qq as lkm_qq6_1_0_,
        linkman0_.lkm_mobile as lkm_mobi7_1_0_,
        linkman0_.lkm_memo as lkm_memo8_1_0_,
        linkman0_.lkm_position as lkm_posi9_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_linkman as cust_lin6_0_0_,
        customer0_.cust_phone as cust_pho7_0_0_,
        customer0_.cust_mobile as cust_mob8_0_0_ 
    from
        cst_customer customer0_ 
    where
        customer0_.cust_id=?
-------1------------
-------2------------
Customer [cust_id=9, cust_name=测试名称3]

 

 

3.fetch为join,lazy属性失效

 

代码:

    // 集合级别的关联
    // join 单表查询
    // lazy: 属性失效
    @Test
    public void fun3() {
        Session session = HibernateUtil.openSession();
        Transaction tx = session.beginTransaction();
        // ----------------------------------------------------
        
        LinkMan linkMan = session.get(LinkMan.class, 5l);
        System.out.println("-------1------------");
        Customer customer = linkMan.getCustomer();
        System.out.println("-------2------------");
        System.out.println(customer);// 打印SQL
        
        // ----------------------------------------------------
        tx.commit();
        session.close();
        
    }

 

SQL:

Hibernate: 
    select
        linkman0_.lkm_id as lkm_id1_1_0_,
        linkman0_.lkm_gender as lkm_gend2_1_0_,
        linkman0_.lkm_name as lkm_name3_1_0_,
        linkman0_.lkm_phone as lkm_phon4_1_0_,
        linkman0_.lkm_email as lkm_emai5_1_0_,
        linkman0_.lkm_qq as lkm_qq6_1_0_,
        linkman0_.lkm_mobile as lkm_mobi7_1_0_,
        linkman0_.lkm_memo as lkm_memo8_1_0_,
        linkman0_.lkm_position as lkm_posi9_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_linkman as cust_lin6_0_1_,
        customer1_.cust_phone as cust_pho7_0_1_,
        customer1_.cust_mobile as cust_mob8_0_1_ 
    from
        cst_linkman linkman0_ 
    left outer join
        cst_customer customer1_ 
            on linkman0_.lkm_cust_id=customer1_.cust_id 
    where
        linkman0_.lkm_id=?
-------1------------
-------2------------
Customer [cust_id=9, cust_name=测试名称3]

 

 

总结:

  为了提高效率.fetch的选择上应选择select. lazy的取值应选择 true. 全部使用默认值.

 

 

3.批量抓取策略(batch-size属性)

  此次略用于获取客户联系人的时候,一次抓取几个客户的联系人。此策略用于避免多次访问数据库,没有此策略是一次获取一个客户联系人,有了此策略可以一次获取多个客户的,减少访问数据库次数。

 

例如

<?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 package="cn.qlq.domain" >
    <class name="Customer" table="cst_customer">
        <id name="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_linkman" column="cust_linkman" ></property>
        <property name="cust_phone" column="cust_phone" ></property>
        <property name="cust_mobile" column="cust_mobile" ></property>
    
    <!-- 
        lazy属性: 决定是否延迟加载
            true(默认值): 延迟加载,懒加载
            false: 立即加载
            extra: 极其懒惰
        fetch属性: 决定加载策略.使用什么类型的sql语句加载集合数据
            select(默认值): 单表查询加载
            join: 使用多表查询加载集合
            subselect:使用子查询加载集合
     -->
     <!-- batch-size: 5   抓取集合的数量为5.
             抓取客户的集合时,一次抓取几个客户的联系人集合.
      -->
        <set name="linkMens" batch-size="5">
            <key column="lkm_cust_id" ></key>
            <one-to-many class="LinkMan" />
        </set>
    </class>
</hibernate-mapping>

 

测试代码:

    public void fun5() {
        Session session = HibernateUtil.openSession();
        Transaction tx = session.beginTransaction();
        // ----------------------------------------------------

        String hql = "from Customer";

        Query query = session.createQuery(hql);

        List<Customer> list = query.list();

        for (Customer c : list) {
            System.out.println(c.getLinkMens());
        }

        // ----------------------------------------------------
        tx.commit();
        session.close();

    }

 

SQL:

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_linkman as cust_lin6_0_,
        customer0_.cust_phone as cust_pho7_0_,
        customer0_.cust_mobile as cust_mob8_0_ 
    from
        cst_customer customer0_
Hibernate: 
    select
        linkmens0_.lkm_cust_id as lkm_cus10_1_1_,
        linkmens0_.lkm_id as lkm_id1_1_1_,
        linkmens0_.lkm_id as lkm_id1_1_0_,
        linkmens0_.lkm_gender as lkm_gend2_1_0_,
        linkmens0_.lkm_name as lkm_name3_1_0_,
        linkmens0_.lkm_phone as lkm_phon4_1_0_,
        linkmens0_.lkm_email as lkm_emai5_1_0_,
        linkmens0_.lkm_qq as lkm_qq6_1_0_,
        linkmens0_.lkm_mobile as lkm_mobi7_1_0_,
        linkmens0_.lkm_memo as lkm_memo8_1_0_,
        linkmens0_.lkm_position as lkm_posi9_1_0_,
        linkmens0_.lkm_cust_id as lkm_cus10_1_0_ 
    from
        cst_linkman linkmens0_ 
    where
        linkmens0_.lkm_cust_id in (
            ?, ?, ?, ?, ?
        )
[LinkMan [lkm_id=4, lkm_gender=null, lkm_name=田七, lkm_phone=null, lkm_email=null, lkm_qq=null, lkm_mobile=null, lkm_memo=null, lkm_position=null, customer=Customer [cust_id=7, cust_name=测试名称1]], LinkMan [lkm_id=3, lkm_gender=null, lkm_name=王八, lkm_phone=null, lkm_email=null, lkm_qq=null, lkm_mobile=null, lkm_memo=null, lkm_position=null, customer=Customer [cust_id=7, cust_name=测试名称1]]]
[]
[LinkMan [lkm_id=5, lkm_gender=null, lkm_name=99, lkm_phone=null, lkm_email=null, lkm_qq=null, lkm_mobile=null, lkm_memo=null, lkm_position=null, customer=Customer [cust_id=9, cust_name=测试名称3]]]
[]
[]
Hibernate: 
    select
        linkmens0_.lkm_cust_id as lkm_cus10_1_1_,
        linkmens0_.lkm_id as lkm_id1_1_1_,
        linkmens0_.lkm_id as lkm_id1_1_0_,
        linkmens0_.lkm_gender as lkm_gend2_1_0_,
        linkmens0_.lkm_name as lkm_name3_1_0_,
        linkmens0_.lkm_phone as lkm_phon4_1_0_,
        linkmens0_.lkm_email as lkm_emai5_1_0_,
        linkmens0_.lkm_qq as lkm_qq6_1_0_,
        linkmens0_.lkm_mobile as lkm_mobi7_1_0_,
        linkmens0_.lkm_memo as lkm_memo8_1_0_,
        linkmens0_.lkm_position as lkm_posi9_1_0_,
        linkmens0_.lkm_cust_id as lkm_cus10_1_0_ 
    from
        cst_linkman linkmens0_ 
    where
        linkmens0_.lkm_cust_id in (
            ?, ?, ?
        )

 

  通过SQL看出获取客户联系人的时候一次获取5个客户的联系人,最后一次由于没有五个客户所以会查询剩余3个客户的联系人。

 

 

 

问题:   no-session问题解决: 扩大session的作用范围.

  在懒加载的策略中,我们通常是在Service层打开事务、session,service代码执行完毕只会进行提交或者回滚。但是我们的对象(代理对象)经常放在request域中带到页面进行显示,也就是到了页面才使用对象(查询数据库),此时session已经关闭,如果使用了懒加载策略会报no-session异常。解决办法:过滤器扩大session作用范围(filter可以在请求执行前后都执行一些代码),如下图逻辑:

  其实SSH整合的时候spring已经做了过滤器来实现此功能,会在搭建SSH环境的时候介绍。

 

posted @ 2018-07-30 14:48  QiaoZhi  阅读(1074)  评论(0编辑  收藏  举报