hibernate學習(6)——載入策略(最佳化)

來源:互聯網
上載者:User

標籤:.class   發送   客戶   ble   getname   highlight   action   方式   org   

1、 檢索方式

1  立即檢索:立即查詢,在執行查詢語句時,立即查詢所有的資料。

2 延遲檢索:延遲查詢,在執行查詢語句之後,在需要時在查詢。(懶載入)

2、  檢查策略

1  類層級檢索:當前的類的屬性擷取是否需要延遲。

2  關聯層級的檢索:當前類 關聯 另一個類是否需要延遲。

3、類層級檢索

1  get:立即檢索。get方法一執行,立即查詢所有欄位的資料。

2 load:延遲檢索。預設情況,load方法執行後,如果只使用OID的值不進行查詢,如果要使用其他屬性值將查詢 。 Customer.hbm.xml  <class  lazy="true | false">

              lazy 預設值true,表示延遲檢索,如果設定false表示立即檢索,相當於get方法。

package com.alice.hibernate02.lazy;import org.hibernate.Session;import org.hibernate.Transaction;import org.junit.Test;import com.alice.hibernate02.util.HibernateUtil;import com.alice.hibernate02.vo.Customer;public class Demo1 {// 類層級懶載入// load方法// class lazy屬性// 預設值: true load獲得時,會返回代理對象,不查詢資料庫.使用時才查詢@Testpublic void test01(){Session session = HibernateUtil.openSession();Transaction tran = session.beginTransaction();Customer cus = (Customer) session.load(Customer.class, 6);System.out.println(cus.getName());tran.commit();session.close();}//類層級懶載入//load方法// class lazy屬性// lazy: false  load方法執行就會發送sql語句.與get方法一致.@Testpublic void test02(){Session session = HibernateUtil.openSession();Transaction tran = session.beginTransaction();Customer cus = (Customer) session.load(Customer.class, 6);System.out.println(cus.getName());tran.commit();session.close();}}
4、  關聯層級檢索

在查詢有關聯關係的資料時,載入一方的資料是否需要將另一方立即查詢出.
預設: 與我關聯的資料,在使用時才會載入.

集合(一對多):
set
lazy: 是否對set資料使用懶載入
        true:(預設值) 對集合使用才載入
        false: 集合將會被立即載入
        extra: 極其懶惰,如果使用集合時,之調用size方法查詢數量, Hibernate會發送count語句,只查詢數量.不載入集合內資料.
fetch : 決定載入集合使用的sql語句種類
         select: (預設值) 普通select查詢
         join: 錶鏈接語句查詢集合資料
         subselect: 使用子查詢 一次載入多個Customer的訂單資料

fetch lazy 結論
select true 預設值, 會在使用集合時載入,普通select語句
select false 立刻使用select語句載入集合資料
select extra 會在使用集合時載入,普通select語句,如果只是獲得集合的長度,會發送Count語句查詢長度
join true 查詢集合時使用錶鏈接查詢,會立刻載入集合資料
join false 查詢集合時使用錶鏈接查詢,會立刻載入集合資料
jojn extra 查詢集合時使用錶鏈接查詢,會立刻載入集合資料
subselect true 會在使用集合時載入,子查詢語句
subselect false 會在查詢使用者時,立即使用子查詢載入客戶的訂單資料
subselect extra 會在使用集合時載入,子查詢語句,如果只是獲得集合的長度,會發送Count語句查詢長度.

代碼測試:

package com.alice.hibernate02.lazy;import java.util.List;import org.hibernate.Session;import org.hibernate.Transaction;import org.junit.Test;import com.alice.hibernate02.util.HibernateUtil;import com.alice.hibernate02.vo.Customer;import com.alice.hibernate02.vo.Order;public class Demo02 {// 關聯層級懶載入// 預設: 與我關聯的資料,在使用時才會載入.@Testpublic void test01() {Session session = HibernateUtil.openSession();Transaction tran = session.beginTransaction();Customer cus = (Customer) session.get(Customer.class, 6);for (Order o : cus.getOrders()) {System.out.println(o.getName());}tran.commit();session.close();}//關聯層級懶載入//lazy: false@Testpublic void test02() {Session session = HibernateUtil.openSession();Transaction tran = session.beginTransaction();Customer cus = (Customer) session.get(Customer.class, 6);for (Order o : cus.getOrders()) {System.out.println(o.getName());}tran.commit();session.close();}//關聯層級懶載入//lazy:false//fetch:subselect@Testpublic void test03() {Session session = HibernateUtil.openSession();Transaction tran = session.beginTransaction();Customer cus = (Customer) session.get(Customer.class, 6);for (Order o : cus.getOrders()) {System.out.println(o.getName());}tran.commit();session.close();}//關聯層級懶載入//lazy:true/false//fetch:subselect@Testpublic void test04() {Session session = HibernateUtil.openSession();Transaction tran = session.beginTransaction();List<Customer> list = session.createQuery("from Customer").list();for (Customer c: list) {System.out.println(c.getName()+"下單數量"+c.getOrders().size());}tran.commit();session.close();}//關聯層級懶載入//lazy:extra//fetch:select@Testpublic void test05() {Session session = HibernateUtil.openSession();Transaction tran = session.beginTransaction();Customer cus = (Customer) session.get(Customer.class, 6);//查詢Customer下訂單 數量System.out.println(cus.getOrders().size());        //真正使用訂單中的資料for (Order o: cus.getOrders()) {System.out.println(o.getName());}tran.commit();session.close();}//關聯層級懶載入//lazy:extra//fetch:subselect@Testpublic void test06() {Session session = HibernateUtil.openSession();Transaction tran = session.beginTransaction();@SuppressWarnings("unchecked")List<Customer> list = session.createQuery("from Customer").list();for(Customer c:list){System.out.println(c.getName()+"下單數量:"+c.getOrders().size());}        for(Customer c:list){        for(Order o :c.getOrders()){        System.out.println(c.getName()+"下單名稱:"+o.getName());        }        }tran.commit();session.close();}}

  多對一:

<many-to-one>

lazy
     false:  載入訂單時,會立即載入客戶
     proxy:看客戶對象的類載入策略來決定
no-proxy : 不做研究.
fetch
     select : (預設值)使用普通select載入

     join : 使用錶鏈接載入資料

 

 

fetch lazy 結果
select false 載入訂單時,立即載入客戶資料.普通select語句載入客戶.
select proxy

類載入策略為:lazy=false 同上
lazy=true載入訂單時,先不載入客戶資料.使用客戶資料時才載入

join false 使用錶鏈接查詢訂單以及對應客戶資訊.lazy屬性無效
join proxy 使用錶鏈接查詢訂單以及對應客戶資訊.lazy屬性無效

 

package com.alice.hibernate02.lazy;import org.hibernate.Session;import org.hibernate.Transaction;import org.junit.Test;import com.alice.hibernate02.util.HibernateUtil;import com.alice.hibernate02.vo.Order;//多對一檢索策略public class Demo03 {    // 多對一檢索策略    @Test    // fetch: select    // lazy: false    public void test01() {        Session session = HibernateUtil.openSession();        Transaction tran = session.beginTransaction();        Order o = (Order) session.get(Order.class, 8);        System.out.println(o.getCustomer().getName());        tran.commit();        session.close();    }    // fetch: select    // lazy: proxy    // Customer lazy:false    @Test    public void test02() {        Session session = HibernateUtil.openSession();        Transaction tran = session.beginTransaction();        Order o = (Order) session.get(Order.class, 8);        System.out.println(o.getCustomer().getName());        tran.commit();        session.close();    }    // fetch: select    // lazy: proxy    // Customer lazy:true    @Test    public void test03() {        Session session = HibernateUtil.openSession();        Transaction tran = session.beginTransaction();        Order o = (Order) session.get(Order.class, 8);        System.out.println(o.getCustomer().getName());        tran.commit();        session.close();    }    // fetch: join    // lazy: proxy|false    @Test    public void test04() {        Session session = HibernateUtil.openSession();        Transaction tran = session.beginTransaction();        Order o = (Order) session.get(Order.class, 8);        System.out.println(o.getCustomer().getName());        tran.commit();        session.close();    }}
5、批量載入

set
batch-size: 決定一次載入幾個對象的集合資料. in 條件載入多個使用者的訂單.

package com.alice.hibernate02.lazy;import java.util.List;import org.hibernate.Session;import org.hibernate.Transaction;import org.junit.Test;import com.alice.hibernate02.util.HibernateUtil;import com.alice.hibernate02.vo.Customer;import com.alice.hibernate02.vo.Order;//批量策略public class Demo04 {    @Test    //查詢所有客戶    //遍曆客戶,列印客戶下的訂單資訊    public void test() {        Session session = HibernateUtil.openSession();        Transaction tran = session.beginTransaction();        List<Customer> list = session.createQuery("from Customer").list();                for(Customer c:list){            System.out.println(c.getOrders().size());        }        tran.commit();        session.close();    }}

 

Customer  Get(int id)

       Return Session.load(Customer.class,id);

  1. layz=false
  2. 在Service層獲得在頁面要上要用到的屬性=> 在Service層中確保資料已經

hibernate學習(6)——載入策略(最佳化)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.