1. 程式人生 > >Hibernate(十三)迫切內連線fetch

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]]]