您好,登錄后才能下訂單哦!
這篇文章主要講解了“Hibernate中加載的類型有幾種”,文中的講解內(nèi)容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“Hibernate中加載的類型有幾種”吧!
一:Hibernate實體對象的加載
比如說:用戶和訂單的關系是一對多,雖然它們有關聯(lián),但是默認是延遲加載Lazy=“true”,最終得到的是代理對象,如果要訪問代理對象的屬性的話,則會拋出異常,
解決方法:leftjoinfetch迫切抓取連接
selectufromUseruleftjoinfetchOrdero;
二:Hibernate普通屬性
默認是:lazy=“false”
如果要改為延遲方法較麻煩
三:Hibernate集合對象
setlistmap默認Lazy=“true”
1:List
Java代碼 <listnamelistname="diarys"table="petDiary"cascade="all"inverse="true"> <keycolumnkeycolumn="petId"></key> <indexcolumnindexcolumn="listindex"></index> <one-to-manyclassone-to-manyclass="com.lovo.po.PetDiary"/> </list> <listnamelistname="diarys"table="petDiary"cascade="all"inverse="true"> <keycolumnkeycolumn="petId"></key> <indexcolumnindexcolumn="listindex"></index> <one-to-manyclassone-to-manyclass="com.lovo.po.PetDiary"/> </list>
2:set
Java代碼 <setnamesetname="orders" table="t_order" cascade="all" inverse="true" lazy="true" > <keycolumnkeycolumn="fk_customer_id"></key> <one-to-manyclassone-to-manyclass="Order"/> </set> <setnamesetname="orders" table="t_order" cascade="all" inverse="true" lazy="true" > <keycolumnkeycolumn="fk_customer_id"></key> <one-to-manyclassone-to-manyclass="Order"/> </set>
3:map
Java代碼 privateMapschool=newHashMap(); publicMapgetSchool(){ returnschool; } publicvoidsetSchool(Mapschool){ this.school=school; } privateMapschool=newHashMap(); publicMapgetSchool(){ returnschool; } publicvoidsetSchool(Mapschool){ this.school=school; }
Xml代碼 <mapnamemapname="school"table="schools"> <keycolumnkeycolumn="pid"not-null="true"/> <map-keytypemap-keytype="string"column="indet"/> <elementtypeelementtype="float"column="score"/> </map> privateMapschool=newHashMap(); publicMapgetSchool(){ returnschool; } publicvoidsetSchool(Mapschool){ this.school=school; } privateMapschool=newHashMap(); publicMapgetSchool(){ returnschool; } publicvoidsetSchool(Mapschool){ this.school=school; }
Xml代碼 <mapnamemapname="school"table="schools"> <keycolumnkeycolumn="pid"not-null="true"/> <map-keytypemap-keytype="string"column="indet"/> <elementtypeelementtype="float"column="score"/> </map>
key子元素用于映射外鍵列,而map-key子元素則用于映射Map集合的Key。
感謝各位的閱讀,以上就是“Hibernate中加載的類型有幾種”的內(nèi)容了,經(jīng)過本文的學習后,相信大家對Hibernate中加載的類型有幾種這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。