itVincent Blog - Java Working Fun!

          技術引領時代!
          posts - 117, comments - 181, trackbacks - 0, articles - 12

          OneToOne lazy loading不生效的問題

          Posted on 2008-11-04 13:38 itVincent 閱讀(2065) 評論(0)  編輯  收藏

          最近項目中遇到了個hibernate lazy延遲加載的問題,實體關系如下A,B
          public class A  {
              @OneToOne(fetch=FetchType.LAZY,mappedBy = "a")
              B b;
          }

          public class B  {
              @OneToOne(fetch=FetchType.LAZY)
              @JoinColumn(name = "A_ID_FK")
              A a;
          }
          情況是:取A的對象時,就算設置了lazy,結果還是eager馬上獲取的, hibernate打印出的也是多了一條SQL語句;
          但當取B的對象時,設置了的lazy就生效了,只有一條SQL語句

          查了不少資料,在robin的文章中找到這樣的話:"先來說說Hibernate吧。Hibernate確實功能強悍,但是Hibernate不夠易用,而且有一些明顯的缺陷:one-to-one必須通過bytecode enhancement才能lazy loading",這里說出OneToOne存在這樣的缺陷,然而所說的bytecode enhancement不是很清楚什么意思,猜測是用cglib進行一些對象的動態改變.

          在論壇中找到這樣的一段解釋:
          Does lazy loading of one-to-one associations work? Lazy loading for
          one-to-one associations is sometimes confusing for new Hibernate users.
          If you consider one-to-one associations based on shared primary keys
          (chapter 7, section 7.1.1, “Shared primary key associations”), an association
          can be proxied only if it’s constrained="true". For example, an
          Address always has a reference to a User. If this association is nullable
          and optional, Hibernate first would have to hit the database to find out
          whether a proxy or a null should be applied—the purpose of lazy loading
          is to not hit the database at all. You can enable lazy loading through
          bytecode instrumentation and interception, which we’ll discuss later.


          綜上的原因得出兩種解決方法:
          1.將OneToOne改為OneToMany,但幾點是改變了實體關系,對已有代碼也有存在影響
          2.將OneToOne的主控方設在你需要lazy loading的實體那里,但兩個都需要lazy loading呢

          You can enable lazy loading through
          bytecode instrumentation and interception, which we’ll discuss later.
          對這句話還要繼續尋找答案


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


          網站導航:
           
          主站蜘蛛池模板: 遂昌县| 错那县| 巴马| 双鸭山市| 满洲里市| 峨边| 汤阴县| 保康县| 花莲县| 宝丰县| 浠水县| 洞口县| 德庆县| 自贡市| 平昌县| 龙门县| 胶州市| 南陵县| 准格尔旗| 巴林右旗| 呼图壁县| 弥勒县| 博罗县| 磴口县| 陈巴尔虎旗| 壶关县| 怀仁县| 汉川市| 鄂温| 广德县| 调兵山市| 石渠县| 乌兰察布市| 云南省| 秦皇岛市| 湖北省| 都江堰市| 兰坪| 曲阳县| 怀仁县| 青浦区|