posts - 73,  comments - 55,  trackbacks - 0

          延遲初始化錯誤是運用Hibernate開發(fā)項目時最常見的錯誤。如果對一個類或者集合配置了延遲檢索策略,那么必須當(dāng)代理類實例或代理集合處于持久化狀態(tài)(即處于Session范圍內(nèi))時,才能初始化它。如果在游離狀態(tài)時才初始化它,就會產(chǎn)生延遲初始化錯誤。

          下面把Customer.hbm.xml文件的<class>元素的lazy屬性設(shè)為true,表示使用延遲檢索策略:

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

          當(dāng)執(zhí)行Session的load()方法時,Hibernate不會立即執(zhí)行查詢CUSTOMERS表的select語句,僅僅返回Customer類的代理類的實例,這個代理類具由以下特征:

          (1) 由Hibernate在運行時動態(tài)生成,它擴展了Customer類,因此它繼承了Customer類的所有屬性和方法,但它的實現(xiàn)對于應(yīng)用程序是透明的。
          (2) 當(dāng)Hibernate創(chuàng)建Customer代理類實例時,僅僅初始化了它的OID屬性,其他屬性都為null,因此這個代理類實例占用的內(nèi)存很少。
          (3) 當(dāng)應(yīng)用程序第一次訪問Customer代理類實例時(例如調(diào)用customer.getXXX()或customer.setXXX()方法), Hibernate會初始化代理類實例,在初始化過程中執(zhí)行select語句,真正從數(shù)據(jù)庫中加載Customer對象的所有數(shù)據(jù)。但有個例外,那就是當(dāng) 應(yīng)用程序訪問Customer代理類實例的getId()方法時,Hibernate不會初始化代理類實例,因為在創(chuàng)建代理類實例時OID就存在了,不必 到數(shù)據(jù)庫中去查詢。

          提示:Hibernate采用CGLIB工具來生成持久化類的代理類。CGLIB是一個功能強大的Java字節(jié)碼生成工具,它能夠在程序運行時動態(tài)生成擴展Java類或者實現(xiàn)Java接口的代理類。關(guān)于CGLIB的更多知識,請參考:http://cglib.sourceforge.net/。

          以下代碼先通過Session的load()方法加載Customer對象,然后訪問它的name屬性:

          tx = session.beginTransaction();
          Customer customer=(Customer)session.load(Customer.class,new Long(1));
          customer.getName();
          tx.commit();

          在 運行session.load()方法時Hibernate不執(zhí)行任何select語句,僅僅返回Customer類的代理類的實例,它的OID為1,這 是由load()方法的第二個參數(shù)指定的。當(dāng)應(yīng)用程序調(diào)用customer.getName()方法時,Hibernate會初始化Customer代理 類實例,從數(shù)據(jù)庫中加載Customer對象的數(shù)據(jù),執(zhí)行以下select語句:

          select * from CUSTOMERS where ID=1;
          select * from ORDERS where CUSTOMER_ID=1;

          當(dāng)<class>元素的lazy屬性為true,會影響Session的load()方法的各種運行時行為,下面舉例說明。

          1.如果加載的Customer對象在數(shù)據(jù)庫中不存在,Session的load()方法不會拋出異常,只有當(dāng)運行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范圍內(nèi),應(yīng)用程序沒有訪問過Customer對象,那么Customer代理類的實例一直不會被初始化,Hibernate不會執(zhí)行任何select語句。以下代碼試圖在關(guān)閉Session后訪問Customer游離對象:

          tx = session.beginTransaction();
          Customer customer=(Customer)session.load(Customer.class,new Long(1));
          tx.commit();
          session.close();
          customer.getName();

          由于引用變量customer引用的Customer代理類的實例在Session范圍內(nèi)始終沒有被初始化,因此在執(zhí)行customer.getName()方法時,Hibernate會拋出以下異常:

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

          由此可見,Customer代理類的實例只有在當(dāng)前Session范圍內(nèi)才能被初始化。

          3.net.sf.hibernate.Hibernate類的initialize()靜態(tài)方法用于在Session范圍內(nèi)顯式初始化代理類實例,isInitialized()方法用于判斷代理類實例是否已經(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代理類實例,因此當(dāng)Session關(guān)閉后,可以正常訪問Customer游離對象。

          4.當(dāng)應(yīng)用程序訪問代理類實例的getId()方法時,不會觸發(fā)Hibernate初始化代理類實例的行為,例如:

          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()方法時,該方法直接返回Customer代理類實例的OID值,無需查詢數(shù)據(jù)庫。由于引用變量 customer始終引用的是沒有被初始化的Customer代理類實例,因此當(dāng)Session關(guān)閉后再執(zhí)行customer.getName()方法, Hibernate會拋出以下異常:

          ERROR LazyInitializer:63 - Exception initializing proxy
          net.sf.hibernate.HibernateException: Could not initialize proxy - the owning Session was closed
          posted on 2006-08-22 09:19 保爾任 閱讀(144) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           

          <2025年5月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          常用鏈接

          留言簿(4)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 平潭县| 绥江县| 苗栗市| 安国市| 沿河| 临澧县| 盐亭县| 普格县| 师宗县| 南雄市| 绩溪县| 黄大仙区| 环江| 阳江市| 屏边| 蒲江县| 乌拉特前旗| 襄汾县| 景洪市| 万全县| 崇信县| 淮南市| 英德市| 曲水县| 五原县| 虎林市| 黔江区| 密云县| 灵璧县| 上蔡县| 华宁县| 新营市| 曲阳县| 石柱| 棋牌| 易门县| 玉山县| 偏关县| 宜君县| 林州市| 大化|