1. 程式人生 > >hibernate 懶載入、延時載入

hibernate 懶載入、延時載入

一、延時載入配置:
下面把Customer.hbm.xml檔案的<class>元素的lazy屬性設為true,表示使用延遲檢索策略:
Xml程式碼
<class name="mypack.Customer" table="CUSTOMERS" lazy="true"> 

<class name="mypack.Customer" table="CUSTOMERS" lazy="true">

二、延時載入特徵:當執行Session的load()方法時,Hibernate不會立即執行查詢CUSTOMERS表的select語句,僅僅返回Customer類的代理類的例項,這個代理類具由以下特徵:

(1) 由Hibernate在執行時動態生成,它擴充套件了Customer類,因此它繼承了Customer類的所有屬性和方法,但它的實現對於應用程式是透明的。
(2) 當Hibernate建立Customer代理類例項時,僅僅初始化了它的OID屬性,其他屬性都為null,因此這個代理類例項佔用的記憶體很少。
(3)當應用程式第一次訪問Customer代理類例項時(例如呼叫customer.getXXX()或customer.setXXX()方法), Hibernate會初始化代理類例項,在初始化過程中執行select語句,真正從資料庫中載入Customer物件的所有資料。但有個例外,那就是當應用程式訪問Customer代理類例項的getId()方法時,Hibernate不會初始化代理類例項,因為在建立代理類例項時OID就存在了,不必到資料庫中去查詢。

三、延時載入例項解析:以下程式碼先通過Session的load()方法載入Customer物件,然後訪問它的name屬性:
Java程式碼
tx = session.beginTransaction(); 
Customer customer=(Customer)session.load(Customer.class,new Long(1)); 
customer.getName(); 
tx.commit(); 


在執行session.load()方法時Hibernate不執行任何select語句,僅僅返回Customer類的代理類的例項,它的OID為1,這是由load()方法的第二個引數指定的。當應用程式呼叫customer.getName()方法時,Hibernate會初始化Customer代理類例項,從資料庫中載入Customer物件的資料,執行以下select語句:
Sql程式碼
select * from CUSTOMERS where ID=1; 
select * from ORDERS where CUSTOMER_ID=1; 


四、延遲初始化錯誤是運用Hibernate開發專案時最常見的錯誤。如果對一個類或者集合配置了延遲檢索策略,那麼必須當代理類例項或代理集合處於持久化狀態(即處於Session範圍內)時,才能初始化它。如果在遊離狀態時才初始化它,就會產生延遲初始化錯誤。當<class>元素的lazy屬性為true,會影響Session的load()方法的各種執行時行為,下面舉例說明。


1.如果載入的Customer物件在資料庫中不存在,Session的load()方法不會丟擲異常,只有當執行customer.getName()方法時才會丟擲以下異常:


ERROR LazyInitializer:63 - Exception initializing proxy
net.sf.hibernate.ObjectNotFoundException: No row with the given identifier exists: 1, of class:
mypack.Customer


2.如果在整個Session範圍內,應用程式沒有訪問過Customer物件,那麼Customer代理類的例項一直不會被初始化,Hibernate不會執行任何select語句。以下程式碼試圖在關閉Session後訪問Customer遊離物件:
Java程式碼
tx = session.beginTransaction(); 
Customer customer=(Customer)session.load(Customer.class,new Long(1)); 
tx.commit(); 
session.close(); 
customer.getName(); 

由於引用變數customer引用的Customer代理類的例項在Session範圍內始終沒有被初始化,因此在執行customer.getName()方法時,Hibernate會丟擲以下異常:

ERROR LazyInitializer:63 - Exception initializing proxy
net.sf.hibernate.HibernateException: Could not initialize proxy - the owning Session was closed

由此可見,Customer代理類的例項只有在當前Session範圍內才能被初始化。


3.net.sf.hibernate.Hibernate類的initialize()靜態方法用於在Session範圍內顯式初始化代理類例項,isInitialized()方法用於判斷代理類例項是否已經被初始化。例如:
Java程式碼
tx = session.beginTransaction(); 
Customer customer=(Customer)session.load(Customer.class,new Long(1)); 
if(!Hibernate.isInitialized(customer)) 
Hibernate.initialize(customer); 
tx.commit(); 
session.close(); 
customer.getName(); 

以上程式碼在Session範圍內通過Hibernate類的initialize()方法顯式初始化了Customer代理類例項,因此當Session關閉後,可以正常訪問Customer遊離物件。


4.當應用程式訪問代理類例項的getId()方法時,不會觸發Hibernate初始化代理類例項的行為,例如:
Java程式碼
tx = session.beginTransaction(); 
Customer customer=(Customer)session.load(Customer.class,new Long(1)); 
customer.getId(); 
tx.commit(); 
session.close(); 
customer.getName(); 

當應用程式訪問customer.getId()方法時,該方法直接返回Customer代理類例項的OID值,無需查詢資料庫。由於引用變數 customer始終引用的是沒有被初始化的Customer代理類例項,因此當Session關閉後再執行customer.getName()方法, Hibernate會丟擲以下異常:


ERROR LazyInitializer:63 - Exception initializing proxy
net.sf.hibernate.HibernateException: Could not initialize proxy - the owning Session was closed


解決方法:

由於hibernate採用了lazy=true,這樣當你用hibernate查詢時,返回實際為利用cglib增強的代理類,但其並沒有實際填充;當你在前端,利用它來取值(getXXX)時,這時Hibernate才會到資料庫執行查詢,並填充物件,但此時如果和這個代理類相關的session已關閉掉,就會產生種錯誤.
在做一對多時,有時會出現"could not initialize proxy - clothe owning Session was sed,這個好像是hibernate的快取問題.問題解決:需要在<many-to-one>裡設定lazy="false". 但有可能會引發另一個異常叫


failed to lazily initialize a collection of role: XXXXXXXX, no session or session was closed


解決方法:在web.xml中加入
Xml程式碼
<filter> 
<filter-name>hibernateFilter</filter-name> 
<filter-class> 
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter 
</filter-class> 
</filter 
<filter-mapping> 
<filter-name>hibernateFilter</filter-name> 
<url-pattern>*.do</url-pattern> 
</filter-mapping> 
五、使用延時載入條件判斷:舉例說明
 hibernate3.0中lazy有三個值,true,false,proxy,預設的是lazy="proxy".具體設定要看你的需求

 假如在student物件中包含一個head物件

 如果你確定在用student物件的時候就要用到head物件裡的屬性,那你就設定立即載入,因為設定立即載入那麼在查詢student的同時就會查詢student的head,hibernate就會在查詢的時候關聯兩張表從而生成的sql就可能只有一條。
 
 而如果你設定的是延遲載入,那麼肯定會要生成1+N條sql語句:其中“1”是查詢student的語句,“N”是根據N個student的id去查詢head的N條語句。而且,延遲載入是要用到的時候才去執行查詢,這樣系統判斷那裡需要載入,那裡不需要載入也需要時間,效能上肯定就不如立即載入了!
 
 如果,你是有的地方需要用到student的時候才用到head屬性,那麼你就設定成延遲載入,因為查詢2張表的資料肯定要比查詢1張表的資料消耗大。
 減少SQL語句,能夠大幅的提高效能;但讀取不需要的屬性,又會浪費記憶體、資料庫和網路資源。
 所以合適的屬性顆度劃分非常重要。一次載入全部需要的屬性,把不需要的屬性延時載入。按照業務需求,把屬性按需求進行切分,確保一次訪問能夠得到全部需要的資料,這是最好的
轉載整理於:http://blog.csdn.net/w183705952/article/details/7082387http://zhidao.baidu.com/question/89445445.html