Hibernate(十三)迫切内连接fetch

Posted DeepSleeping丶

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hibernate(十三)迫切内连接fetch相关的知识,希望对你有一定的参考价值。

迫切内连接fetch

内连接和迫切内连接的区别:

  其主要区别就在于封装数据,因为他们查询的结果集都是一样的,生成底层的SQL语句也是一样的。

    1.内连接:发送就是内连接的语句,封装的时候将属于各自对象的数据封装到各自的对象中,最后得到一个List<Object[]>

    2.迫切内连接:发送的是内连接的语句,需要在编写HQL的时候再join后添加一个fetch关键字,Hibernate会发送HQL中的fetch关键字,从而将每条数据封装到对象中,最后得到一个List<Customer>

    但是,迫切内连接封装以后会出现重复的数据,因为假设我们查询到目前有三条记录,就会被封装到三个对象中,其实我们真正的用户对象只有两个,所以往往自己在手动编写迫切内连接的时候会使用distinct去掉重复值。

普通内连接,就是将用户customer,和关系的订单ods,分成两个object对象返回。

/**
     * 普通内连接
     */
    @Test
    public void fun(){
        Session session = HibernateUtils.getSession();
        Transaction tx = session.beginTransaction();
        
        List<Object[]> list = session.createQuery("from Customer cst inner join cst.ods").list();
        for (Object[] objects : list) {
            System.out.println(Arrays.toString(objects));
        }
        
        tx.commit();
        session.close();
    }

[Customer [cust_id=8, cust_name=台用胜, cust_gender=男, cust_age=24, cust_phone=18736549531, ods=[[email protected], [email protected]]], [email protected]]
[Customer [cust_id=8, cust_name=台用胜, cust_gender=男, cust_age=24, cust_phone=18736549531, ods=[[email protected], [email protected]]], [email protected]]

而迫切连接就是将用户customer中的订单ods封装进customer中的ods属性中,一起返回

/**
     * 迫切内连接
     */
    @Test
    public void fun1(){
        Session session = HibernateUtils.getSession();
        Transaction tx = session.beginTransaction();
        
        List<Customer> list = session.createQuery("from Customer cst inner join fetch cst.ods").list();
        for(Customer cst : list){
            System.out.println(cst);
        }
        
        tx.commit();
        session.close();
    }

 

Hibernate:
    select
        customer0_.cust_id as cust_id1_0_0_,
        ods1_.order_id as order_id1_3_1_,
        customer0_.cust_name as cust_nam2_0_0_,
        customer0_.cust_gender as cust_gen3_0_0_,
        customer0_.cust_age as cust_age4_0_0_,
        customer0_.cust_phone as cust_pho5_0_0_,
        ods1_.detail_id as detail_i2_3_1_,
        ods1_.cust_order_id as cust_ord3_3_1_,
        ods1_.cust_order_id as cust_ord3_3_0__,
        ods1_.order_id as order_id1_3_0__
    from
        customera customer0_
    inner join
        ordersa ods1_
            on customer0_.cust_id=ods1_.cust_order_id
Customer [cust_id=6, cust_name=台用胜, cust_gender=男, cust_age=24, cust_phone=18736549531, ods=[[email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected]]]
Customer [cust_id=6, cust_name=台用胜, cust_gender=男, cust_age=24, cust_phone=18736549531, ods=[[email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected]]]
Customer [cust_id=6, cust_name=台用胜, cust_gender=男, cust_age=24, cust_phone=18736549531, ods=[[email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected]]]
Customer [cust_id=6, cust_name=台用胜, cust_gender=男, cust_age=24, cust_phone=18736549531, ods=[[email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected]]]
Customer [cust_id=6, cust_name=台用胜, cust_gender=男, cust_age=24, cust_phone=18736549531, ods=[[email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected]]]
Customer [cust_id=6, cust_name=台用胜, cust_gender=男, cust_age=24, cust_phone=18736549531, ods=[[email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected]]]
Customer [cust_id=6, cust_name=台用胜, cust_gender=男, cust_age=24, cust_phone=18736549531, ods=[[email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected]]]
Customer [cust_id=7, cust_name=台用胜, cust_gender=男, cust_age=24, cust_phone=18736549531, ods=[[email protected], [email protected]]]
Customer [cust_id=7, cust_name=台用胜, cust_gender=男, cust_age=24, cust_phone=18736549531, ods=[[email protected], [email protected]]]
Customer [cust_id=8, cust_name=台用胜, cust_gender=男, cust_age=24, cust_phone=18736549531, ods=[[email protected], [email protected]]]
Customer [cust_id=8, cust_name=台用胜, cust_gender=男, cust_age=24, cust_phone=18736549531, ods=[[email protected], [email protected]]]
Customer [cust_id=10, cust_name=台用胜, cust_gender=男, cust_age=24, cust_phone=18736549531, ods=[[email protected], [email protected]]]
Customer [cust_id=10, cust_name=台用胜, cust_gender=男, cust_age=24, cust_phone=18736549531, ods=[[email protected], [email protected]]]
Customer [cust_id=11, cust_name=台用胜, cust_gender=男, cust_age=24, cust_phone=18736549531, ods=[[email protected], [email protected]]]
Customer [cust_id=11, cust_name=台用胜, cust_gender=男, cust_age=24, cust_phone=18736549531, ods=[[email protected], [email protected]]]

 



































以上是关于Hibernate(十三)迫切内连接fetch的主要内容,如果未能解决你的问题,请参考以下文章

hibernate的多表查询

Hibernate 迫切连接和普通连接的区别

[原创]java WEB学习笔记88:Hibernate学习之路-- -Hibernate检索策略(立即检索,延迟检索,迫切左外连接检索)

Hibernate5-一对多双向关联-迫切左外连接-HQL

Hibernate HQL ③

HQL连接查询和注解