itVincent Blog - Java Working Fun!

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

          OneToOne lazy loading不生效的問題

          Posted on 2008-11-04 13:38 itVincent 閱讀(2056) 評論(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.
          對這句話還要繼續尋找答案


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


          網站導航:
           
          主站蜘蛛池模板: 内乡县| 金寨县| 邢台市| 晋州市| 定南县| 白玉县| 金堂县| 德江县| 紫阳县| 井研县| 岳西县| 河源市| 萍乡市| 阳西县| 盱眙县| 吴堡县| 聊城市| 会泽县| 三穗县| 炎陵县| 临澧县| 霍林郭勒市| 开封市| 丽江市| 营口市| 安阳市| 汽车| 青州市| 华蓥市| 工布江达县| 宣武区| 文登市| 乳山市| 胶州市| 青田县| 高邮市| 峨山| 崇左市| 仙桃市| 天长市| 辽阳市|