<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 165, comments - 198, trackbacks - 0, articles - 1
      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

    Hibernate fetch lazy cascade inverse 關鍵字(轉)

    Posted on 2008-07-23 11:14 G_G 閱讀(966) 評論(0)  編輯  收藏 所屬分類: hibernate
    一篇非常好的 Hibernate? 文章
    出自: http://guxing.blog.enorth.com.cn/article/174717.shtml

    Tags: fetch
    Hibernate最讓人頭大的就是對集合的加載形式。

    書看了N次了,還是沒有真正理解Hibernate。所以下午專門做了下測試,對配置文件的意思加深了認識。

    假設有兩個表,Photos(一)? ---? picture(多)Photo包含picture集合

    結論1: HQL代碼 > fetch(配置) > lazy (配置)
    結論2: 默認 lazy="true"
    結論3: fetch 和 lazy 主要是用來級聯查詢的,?? 而 cascade 和 inverse 主要是用來級聯插入和修改的
    結論4: 如果你是用spring來幫你管理你的session, 并且是自動提交,延遲加載就等于沒加載~_~(當然除非你手動重新打開session然后手動Hibernate.initialize(set);然后關閉session.
    結論5:? cascade主要是簡化了在代碼中的級聯更新和刪除。
    j結論6:老爸可以有多個孩子,一個孩子不能有多個老爸,而且老爸說的算, 孩子圍著老爸轉。所以Photos老爸要有權力所以 cascade?這個關鍵子都是送給老爸的,?也就是級聯更新,老爸改姓了,兒子也得跟著改,呵呵?!安蝗唬蜎]有零花錢咯”。而Picture兒子整體挨罵,但是還是要維護父子之間良好的關系,對老爸百依百順,所以老爸就說,兒子,“關系,由你來維護(inverse="true")?,不然就不給零花錢。呵。”。
    ?????????????? <set name="pictures" inverse="true" cascade="all">
    ??????????????????? <key>
    ?????????????????????? <column name="photosid" not-null="true" />
    ??????????????????? </key>
    ???????????????? <one-to-many class="girl.domain.Picture" />
    ???????? ??? </set>

    ???????????????
    測試代碼:

    ???Photos p = ps.getById(1);
    ??Set<Picture> set = p.getPictures();
    ??for(Picture pic : set){
    ? ???System.out.println(pic.getId());
    ??}

    ? 配置文件的一部分:
    ?????? <set name="pictures" inverse="true" cascade="all" >
    ??????????? <key>
    ??????????????? <column name="photosid" not-null="true" />
    ??????????? </key>
    ??????????? <one-to-many class="girl.domain.Picture" />
    ??????? </set>


    測試過程會對配置文件不斷修改:并且從來不曾手動重新打開session

    測試結構:

    當配置條件為 lazy=true一句查詢 測試代碼中沒有調用getPicture() ?正常
    Hibernate: select photos0_.id as id0_0_, photos0_.userid as userid0_0_, photos0_.typeid as typeid0_0_, photos0_.name as name0_0_, photos0_.createtime as createtime0_0_, photos0_.description as descript6_0_0_, photos0_.faceid as faceid0_0_, photos0_.uri as uri0_0_ from super.photos photos0_ where photos0_.id=?

    lazy=true 一句查詢 有getPicture()
    Hibernate: select photos0_.id as id0_0_, photos0_.userid as userid0_0_, photos0_.typeid as typeid0_0_, photos0_.name as name0_0_, photos0_.createtime as createtime0_0_, photos0_.description as descript6_0_0_, photos0_.faceid as faceid0_0_, photos0_.uri as uri0_0_ from super.photos photos0_ where photos0_.id=?


    lazy=true
    一句查詢? 有getPicture() 并且訪問了里面的元數Picture 且有異常拋出
    Hibernate: select photos0_.id as id0_0_, photos0_.userid as userid0_0_, photos0_.typeid as typeid0_0_, photos0_.name as name0_0_, photos0_.createtime as createtime0_0_, photos0_.description as descript6_0_0_, photos0_.faceid as faceid0_0_, photos0_.uri as uri0_0_ from super.photos photos0_ where photos0_.id=?


    lazy="false" 兩句查詢? 肯定沒問題,因為全部數據都個查了出來 所以怎么調用都正常
    Hibernate: select photos0_.id as id0_0_, photos0_.userid as userid0_0_, photos0_.typeid as typeid0_0_, photos0_.name as name0_0_, photos0_.createtime as createtime0_0_, photos0_.description as descript6_0_0_, photos0_.faceid as faceid0_0_, photos0_.uri as uri0_0_ from super.photos photos0_ where photos0_.id=?
    Hibernate: select pictures0_.photosid as photosid1_, pictures0_.id as id1_, pictures0_.id as id2_0_, pictures0_.photosid as photosid2_0_, pictures0_.name as name2_0_, pictures0_.clicked as clicked2_0_, pictures0_.uploaddate as uploaddate2_0_, pictures0_.size as size2_0_, pictures0_.description as descript7_2_0_, pictures0_.uri as uri2_0_ from super.picture pictures0_ where pictures0_.photosid=?


    fetch="join"?
    一句查詢? 效果 == lazy="false" 呵呵,哪個效率高,我就不知道了。。。。。。。。。。。
    Hibernate: select photos0_.id as id0_1_, photos0_.userid as userid0_1_, photos0_.typeid as typeid0_1_, photos0_.name as name0_1_, photos0_.createtime as createtime0_1_, photos0_.description as descript6_0_1_, photos0_.faceid as faceid0_1_, photos0_.uri as uri0_1_, pictures1_.photosid as photosid3_, pictures1_.id as id3_, pictures1_.id as id2_0_, pictures1_.photosid as photosid2_0_, pictures1_.name as name2_0_, pictures1_.clicked as clicked2_0_, pictures1_.uploaddate as uploaddate2_0_, pictures1_.size as size2_0_, pictures1_.description as descript7_2_0_, pictures1_.uri as uri2_0_ from super.photos photos0_ left outer join super.picture pictures1_ on photos0_.id=pictures1_.photosid where photos0_.id=?

    不加fetch="join"一句查詢? 沒有getPicture() 正常
    Hibernate: select photos0_.id as id0_0_, photos0_.userid as userid0_0_, photos0_.typeid as typeid0_0_, photos0_.name as name0_0_, photos0_.createtime as createtime0_0_, photos0_.description as descript6_0_0_, photos0_.faceid as faceid0_0_, photos0_.uri as uri0_0_ from super.photos photos0_ where photos0_.id=?

    不加fetch="join" 一句查詢? 有getPicture() 正常
    Hibernate: select photos0_.id as id0_0_, photos0_.userid as userid0_0_, photos0_.typeid as typeid0_0_, photos0_.name as name0_0_, photos0_.createtime as createtime0_0_, photos0_.description as descript6_0_0_, photos0_.faceid as faceid0_0_, photos0_.uri as uri0_0_ from super.photos photos0_ where photos0_.id=?

    不加fetch="join"一句查詢 有getPicture() 并且訪問里面的元素Picture的ID 有異常拋出
    Hibernate: select photos0_.id as id0_0_, photos0_.userid as userid0_0_, photos0_.typeid as typeid0_0_, photos0_.name as name0_0_, photos0_.createtime as createtime0_0_, photos0_.description as descript6_0_0_, photos0_.faceid as faceid0_0_, photos0_.uri as uri0_0_ from super.photos photos0_ where photos0_.id=?

    來個兩兵交戰 fetch="join" lazy="true"? 呵呵 結果,一句查詢, 結構正常 所以就當lazy不存在好了。 看來fetch 是老大。、、、、、、、、、、、、、
    Hibernate: select photos0_.id as id0_1_, photos0_.userid as userid0_1_, photos0_.typeid as typeid0_1_, photos0_.name as name0_1_, photos0_.createtime as createtime0_1_, photos0_.description as descript6_0_1_, photos0_.faceid as faceid0_1_, photos0_.uri as uri0_1_, pictures1_.photosid as photosid3_, pictures1_.id as id3_, pictures1_.id as id2_0_, pictures1_.photosid as photosid2_0_, pictures1_.name as name2_0_, pictures1_.clicked as clicked2_0_, pictures1_.uploaddate as uploaddate2_0_, pictures1_.size as size2_0_, pictures1_.description as descript7_2_0_, pictures1_.uri as uri2_0_ from super.photos photos0_ left outer join super.picture pictures1_ on photos0_.id=pictures1_.photosid where photos0_.id=?



    主站蜘蛛池模板: ZZIJZZIJ亚洲日本少妇JIZJIZ| 免费无遮挡无码视频网站| 亚洲最大AV网站在线观看| 一区二区免费在线观看| 亚洲精品国产精品国自产观看| 大桥未久亚洲无av码在线 | 精品熟女少妇a∨免费久久| 亚洲无线码在线一区观看| 国产精品hd免费观看| 亚洲午夜无码久久久久| 在线观看片免费人成视频无码| 亚洲AV区无码字幕中文色| 一级毛片在线观看免费| 亚洲国产情侣一区二区三区| 日韩毛片免费无码无毒视频观看| 在线观看亚洲AV每日更新无码| 破了亲妺妺的处免费视频国产| 日韩精品亚洲专区在线影视| 亚洲人成网站在线观看青青| 国产精品区免费视频| 亚洲国产精品综合久久网各| 亚洲国产成人五月综合网| WWW国产成人免费观看视频| 亚洲国产精品无码久久久秋霞2 | 外国成人网在线观看免费视频| 亚洲av无码不卡| 国产四虎免费精品视频| 亚洲国产成人手机在线观看| 亚洲AⅤ永久无码精品AA| 亚洲国产亚洲片在线观看播放| 成年男女男精品免费视频网站| 视频免费1区二区三区| 亚洲av最新在线网址| 91情侣在线精品国产免费| 精品视频免费在线| 亚洲福利在线视频| 麻豆精品国产免费观看| 最近2019中文免费字幕在线观看| 亚洲二区在线视频| 国产91精品一区二区麻豆亚洲 | 成人特黄a级毛片免费视频|