zoukankan      html  css  js  c++  java
  • 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=[deep.domain.Order@133e019b, deep.domain.Order@41382722]], deep.domain.Order@133e019b]
    [Customer [cust_id=8, cust_name=台用胜, cust_gender=男, cust_age=24, cust_phone=18736549531, ods=[deep.domain.Order@133e019b, deep.domain.Order@41382722]], deep.domain.Order@41382722]

    而迫切连接就是将用户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=[deep.domain.Order@2a7b6f69, deep.domain.Order@20312893, deep.domain.Order@4e9658b5, deep.domain.Order@3113a37, deep.domain.Order@600b0b7, deep.domain.Order@213e3629, deep.domain.Order@47747fb9]]
    Customer [cust_id=6, cust_name=台用胜, cust_gender=男, cust_age=24, cust_phone=18736549531, ods=[deep.domain.Order@2a7b6f69, deep.domain.Order@20312893, deep.domain.Order@4e9658b5, deep.domain.Order@3113a37, deep.domain.Order@600b0b7, deep.domain.Order@213e3629, deep.domain.Order@47747fb9]]
    Customer [cust_id=6, cust_name=台用胜, cust_gender=男, cust_age=24, cust_phone=18736549531, ods=[deep.domain.Order@2a7b6f69, deep.domain.Order@20312893, deep.domain.Order@4e9658b5, deep.domain.Order@3113a37, deep.domain.Order@600b0b7, deep.domain.Order@213e3629, deep.domain.Order@47747fb9]]
    Customer [cust_id=6, cust_name=台用胜, cust_gender=男, cust_age=24, cust_phone=18736549531, ods=[deep.domain.Order@2a7b6f69, deep.domain.Order@20312893, deep.domain.Order@4e9658b5, deep.domain.Order@3113a37, deep.domain.Order@600b0b7, deep.domain.Order@213e3629, deep.domain.Order@47747fb9]]
    Customer [cust_id=6, cust_name=台用胜, cust_gender=男, cust_age=24, cust_phone=18736549531, ods=[deep.domain.Order@2a7b6f69, deep.domain.Order@20312893, deep.domain.Order@4e9658b5, deep.domain.Order@3113a37, deep.domain.Order@600b0b7, deep.domain.Order@213e3629, deep.domain.Order@47747fb9]]
    Customer [cust_id=6, cust_name=台用胜, cust_gender=男, cust_age=24, cust_phone=18736549531, ods=[deep.domain.Order@2a7b6f69, deep.domain.Order@20312893, deep.domain.Order@4e9658b5, deep.domain.Order@3113a37, deep.domain.Order@600b0b7, deep.domain.Order@213e3629, deep.domain.Order@47747fb9]]
    Customer [cust_id=6, cust_name=台用胜, cust_gender=男, cust_age=24, cust_phone=18736549531, ods=[deep.domain.Order@2a7b6f69, deep.domain.Order@20312893, deep.domain.Order@4e9658b5, deep.domain.Order@3113a37, deep.domain.Order@600b0b7, deep.domain.Order@213e3629, deep.domain.Order@47747fb9]]
    Customer [cust_id=7, cust_name=台用胜, cust_gender=男, cust_age=24, cust_phone=18736549531, ods=[deep.domain.Order@c41709a, deep.domain.Order@7db0565c]]
    Customer [cust_id=7, cust_name=台用胜, cust_gender=男, cust_age=24, cust_phone=18736549531, ods=[deep.domain.Order@c41709a, deep.domain.Order@7db0565c]]
    Customer [cust_id=8, cust_name=台用胜, cust_gender=男, cust_age=24, cust_phone=18736549531, ods=[deep.domain.Order@52eacb4b, deep.domain.Order@5528a42c]]
    Customer [cust_id=8, cust_name=台用胜, cust_gender=男, cust_age=24, cust_phone=18736549531, ods=[deep.domain.Order@52eacb4b, deep.domain.Order@5528a42c]]
    Customer [cust_id=10, cust_name=台用胜, cust_gender=男, cust_age=24, cust_phone=18736549531, ods=[deep.domain.Order@1edb61b1, deep.domain.Order@1a6f5124]]
    Customer [cust_id=10, cust_name=台用胜, cust_gender=男, cust_age=24, cust_phone=18736549531, ods=[deep.domain.Order@1edb61b1, deep.domain.Order@1a6f5124]]
    Customer [cust_id=11, cust_name=台用胜, cust_gender=男, cust_age=24, cust_phone=18736549531, ods=[deep.domain.Order@29539e36, deep.domain.Order@cc62a3b]]
    Customer [cust_id=11, cust_name=台用胜, cust_gender=男, cust_age=24, cust_phone=18736549531, ods=[deep.domain.Order@29539e36, deep.domain.Order@cc62a3b]]

  • 相关阅读:
    诸葛亮的后半生:狗笼子里挥舞丈八蛇矛
    一句话摘录
    【书摘】The Joshua tree epiphany
    玩具程序:bigInt
    旅行的力量
    记忆的力量
    快的力量
    Windbg学习笔记【4】
    戴尔笔记本win8全新安装
    悟透JavaScript
  • 原文地址:https://www.cnblogs.com/deepSleeping/p/10012658.html
Copyright © 2011-2022 走看看