Hibernate延時(shí)加載,其實(shí)這個(gè)異常寫的非常之清楚,就是會(huì)話關(guān)閉,無法對(duì)Hibernate實(shí)體進(jìn)行操作。造成這樣的情況有很多,什么書寫錯(cuò)誤啊,邏輯錯(cuò)誤啊。
但就此說一下關(guān)于lazy機(jī)制:
Hibernate延時(shí)加載包括延遲初始化錯(cuò)誤,這是運(yùn)用Hibernate開發(fā)項(xiàng)目時(shí)最常見的錯(cuò)誤。如果對(duì)一個(gè)類或者集合配置了延遲檢索策略,那么必須當(dāng)代理類實(shí)例或代理集合處于持久化狀態(tài)(即處于Session范圍內(nèi))時(shí),才能初始化它。如果在游離狀態(tài)時(shí)才初始化它,就會(huì)產(chǎn)生延遲初始化錯(cuò)誤。
下面把Customer.hbm.xml文件的< class>元素的lazy屬性設(shè)為true,表示使用延遲檢索策略:
- < class name="mypack.Customer" table="CUSTOMERS" lazy="true">??
當(dāng)執(zhí)行Session的load()方法時(shí),Hibernate不會(huì)立即執(zhí)行查詢CUSTOMERS表的select語句,僅僅返回Customer類的代理類的實(shí)例,這個(gè)代理類具由以下特征:
(1) 由Hibernate在運(yùn)行時(shí)動(dòng)態(tài)生成,它擴(kuò)展了Customer類,因此它繼承了Customer類的所有屬性和方法,但它的實(shí)現(xiàn)對(duì)于應(yīng)用程序是透明的。
(2) 當(dāng)Hibernate創(chuàng)建Customer代理類實(shí)例時(shí),僅僅初始化了它的OID屬性,其他屬性都為null,因此這個(gè)代理類實(shí)例占用的內(nèi)存很少。
(3)當(dāng)應(yīng)用程序第一次訪問Customer代理類實(shí)例時(shí)(例如調(diào)用customer.getXXX()或customer.setXXX ()方法), Hibernate會(huì)初始化代理類實(shí)例,在初始化過程中執(zhí)行select語句,真正從數(shù)據(jù)庫中加載Customer對(duì)象的所有數(shù)據(jù)。但有個(gè)例外,那就是當(dāng) 應(yīng)用程序訪問Customer代理類實(shí)例的getId()方法時(shí),Hibernate不會(huì)初始化代理類實(shí)例,因?yàn)樵趧?chuàng)建代理類實(shí)例時(shí)OID就存在了,不必 到數(shù)據(jù)庫中去查詢。
提示:Hibernate采用CGLIB工具來生成持久化類的代理類。CGLIB是一個(gè)功能強(qiáng)大的Java字節(jié)碼生成工具,它能夠在程序運(yùn)行時(shí)動(dòng)態(tài)生成擴(kuò) 展 Java類或者實(shí)現(xiàn)Java接口的代理類。
以下代碼先通過Session的load()方法加載Customer對(duì)象,然后訪問它的name屬性:
- tx = session.beginTransaction();
- Customer customer=(Customer)session.load(Customer.class,new Long(1));
- customer.getName();
- tx.commit();??
在運(yùn)行session.load ()方 法時(shí)Hibernate不執(zhí)行任何select語句,僅僅返回Customer類的代理類的實(shí)例,它的OID為1,這是由load()方法的第二個(gè) 參數(shù)指定的。當(dāng)應(yīng)用程序調(diào)用customer.getName()方法時(shí),Hibernate會(huì)初始化Customer代理類實(shí)例,從數(shù)據(jù)庫中加載 Customer對(duì)象的數(shù)據(jù),執(zhí)行以下select語句:
- select * from CUSTOMERS where ID=1;
- select * from ORDERS where CUSTOMER_ID=1;??
當(dāng)< class>元素的lazy屬性為true,會(huì)影響Session的load()方法的各種運(yùn)行時(shí)行為,下面舉例說明。
1.如果加載的Customer對(duì)象在數(shù)據(jù)庫中不存在,Session的load()方法不會(huì)拋出異常,只有當(dāng)運(yùn)行customer.getName()方法時(shí)才會(huì)拋出以下異常:
- ERROR LazyInitializer:63 - Exception initializing proxy
- net.sf.hibernate.ObjectNotFoundException: No row with the given identifier exists: 1, of class:
- mypack.Customer??
2.如果在整個(gè)Session范圍內(nèi),應(yīng)用程序沒有訪問過Customer對(duì)象,那么Customer代理類的實(shí)例一直不會(huì)被初始化,Hibernate不會(huì)執(zhí)行任何select語句。以下代碼試圖在關(guān)閉Session后訪問Customer游離對(duì)象:
- tx = session.beginTransaction();
- Customer customer=(Customer)session.load(Customer.class,new Long(1));
- tx.commit();
- session.close();
- customer.getName();??
由于引用變量customer引用的Customer代理類的實(shí)例在Session范圍內(nèi)始終沒有被初始化,因此在執(zhí)行customer.getName()方法時(shí),Hibernate會(huì)拋出以下異常(Hibernate延時(shí)加載的問題之一):
- ERROR LazyInitializer:63 - Exception initializing proxy
- net.sf.hibernate.HibernateException: Couldnotinitializeproxy-theowningSessionwasclosed??
由此可見,Customer代理類的實(shí)例只有在當(dāng)前Session范圍內(nèi)才能被初始化。
3.net.sf.hibernate.Hibernate類的initialize()靜態(tài)方法用于在Session范圍內(nèi)顯式初始化代理類實(shí)例,isInitialized()方法用于判斷代理類實(shí)例是否已經(jīng)被初始化。例如:
- 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范圍內(nèi)通過Hibernate類的initialize()方法顯式初始化了Customer代理類實(shí)例,因此當(dāng)Session關(guān)閉后,可以正常訪問Customer游離對(duì)象。
4.當(dāng)應(yīng)用程序訪問代理類實(shí)例的getId()方法時(shí),不會(huì)觸發(fā)Hibernate初始化代理類實(shí)例的行為,例如:
- tx = session.beginTransaction();
- Customer customer=(Customer)session.load(Customer.class,new Long(1));
- customer.getId();
- tx.commit();
- session.close();
- customer.getName();??
當(dāng)應(yīng)用程序訪問customer.getId()方法時(shí),該方法直接返回Customer代理類實(shí)例的OID值,無需查詢數(shù)據(jù)庫。由于引用變量 customer始終引用的是沒有被初始化的Customer代理類實(shí)例,因此當(dāng)Session關(guān)閉后再執(zhí)行customer.getName()方法, Hibernate會(huì)拋出以下異常(Hibernate延時(shí)加載的問題之一):
- ERROR LazyInitializer:63 - Exception initializing proxy
- net.sf.hibernate.HibernateException: Couldnotinitializeproxy-theowningSessionwasclosed??
解決方法:
由于hibernate采用了lazy=true,這樣當(dāng)你用hibernate查詢時(shí),返回實(shí)際為利用cglib增強(qiáng)的代理類,但其并沒有實(shí)際填 充;當(dāng)你在前端,利用它來取值(getXXX)時(shí),這時(shí)Hibernate才會(huì)到數(shù)據(jù)庫執(zhí)行查詢,并填充對(duì)象,但此時(shí)如果和這個(gè)代理類相關(guān)的session已關(guān)閉掉,就會(huì)產(chǎn)生種錯(cuò)誤.
在做一對(duì)多時(shí),有時(shí)會(huì)出現(xiàn)"could not initialize proxy - clothe owning Session was sed,這個(gè)好像是hibernate的緩存問題.問題解決:需要在< many-to-one>里設(shè)置lazy="false". 但有可能會(huì)引發(fā)另一個(gè)異常叫
- failed to lazily initialize a collection of role: XXXXXXXX, no session or session was closed??
解決方法:在web.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>
就可以了。
以上文章轉(zhuǎn)自:http://developer.51cto.com/art/200907/133249.htm
關(guān)鍵字:Hibernate,延時(shí)加載,lazy
posted on 2009-07-03 13:39
jadmin 閱讀(79)
評(píng)論(0) 編輯 收藏