自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

淺談Hibernate中加載的三種類型

開發(fā) 后端
本文將介紹Hibernate加載的三種類型,包括加載實(shí)體對象,普通屬性的加載和集合對象的加載。Hibernate加載數(shù)據(jù)庫也是大家工作的重點(diǎn)。

一:Hibernate實(shí)體對象的加載

比如說:用戶和訂單的關(guān)系是一對多,雖然它們有關(guān)聯(lián),但是默認(rèn)是延遲加載Lazy=“true”,最終得到的是代理對象,如果要訪問代理對象的屬性的話,則會拋出異常,

解決方法:leftjoinfetch迫切抓取連接

selectufromUseruleftjoinfetchOrdero;

二:Hibernate普通屬性

默認(rèn)是:lazy=“false”

如果要改為延遲方法較麻煩

三:Hibernate集合對象

setlistmap默認(rèn)Lazy=“true”

1:List

  1. Java代碼  
  2. <listnamelistname="diarys"table="petDiary"cascade="all"inverse="true"> 
  3. <keycolumnkeycolumn="petId"></key> 
  4. <indexcolumnindexcolumn="listindex"></index> 
  5. <one-to-manyclassone-to-manyclass="com.lovo.po.PetDiary"/> 
  6. </list> 
  7.  
  8. <listnamelistname="diarys"table="petDiary"cascade="all"inverse="true"> 
  9.   <keycolumnkeycolumn="petId"></key> 
  10.   <indexcolumnindexcolumn="listindex"></index> 
  11.   <one-to-manyclassone-to-manyclass="com.lovo.po.PetDiary"/> 
  12. </list> 

2:set

  1. Java代碼  
  2. <setnamesetname="orders" 
  3. table="t_order" 
  4. cascade="all" 
  5. inverse="true" 
  6. lazy="true" 
  7. > 
  8. <keycolumnkeycolumn="fk_customer_id"></key> 
  9. <one-to-manyclassone-to-manyclass="Order"/> 
  10. </set> 
  11.  
  12. <setnamesetname="orders" 
  13. table="t_order" 
  14. cascade="all" 
  15. inverse="true" 
  16. lazy="true" 
  17. > 
  18. <keycolumnkeycolumn="fk_customer_id"></key> 
  19. <one-to-manyclassone-to-manyclass="Order"/> 
  20. </set> 

3:map

  1. Java代碼  
  2. privateMapschool=newHashMap();  
  3.  
  4. publicMapgetSchool(){  
  5. returnschool;  
  6. }  
  7. publicvoidsetSchool(Mapschool){  
  8. this.school=school;  
  9. }  
  10.  
  11. privateMapschool=newHashMap();  
  12.  
  13. publicMapgetSchool(){  
  14. returnschool;  
  15. }  
  16. publicvoidsetSchool(Mapschool){  
  17. this.school=school;  
  1. Xml代碼  
  2. <mapnamemapname="school"table="schools"> 
  3. <keycolumnkeycolumn="pid"not-null="true"/> 
  4. <map-keytypemap-keytype="string"column="indet"/> 
  5. <elementtypeelementtype="float"column="score"/> 
  6. </map> 
  7.  
  8. privateMapschool=newHashMap();  
  9.  
  10. publicMapgetSchool(){  
  11. returnschool;  
  12. }  
  13. publicvoidsetSchool(Mapschool){  
  14. this.school=school;  
  15. }  
  16.  
  17. privateMapschool=newHashMap();  
  18.  
  19. publicMapgetSchool(){  
  20. returnschool;  
  21. }  
  22. publicvoidsetSchool(Mapschool){  
  23. this.school=school;  
  1. Xml代碼  
  2. <mapnamemapname="school"table="schools"> 
  3. <keycolumnkeycolumn="pid"not-null="true"/> 
  4. <map-keytypemap-keytype="string"column="indet"/> 
  5. <elementtypeelementtype="float"column="score"/> 
  6. </map> 

key子元素用于映射外鍵列,而map-key子元素則用于映射Map集合的Key。

【編輯推薦】

  1. Hibernate中g(shù)enerator屬性的意義
  2. hibernate Key Generator 主鍵生成方式
  3. Hibernate的主鍵生成機(jī)制
  4. hibernate的Query cache
  5. Hibernate中hbm的generator屬性
責(zé)任編輯:彭凡 來源: javaeye
相關(guān)推薦

2011-01-18 15:35:59

jQueryJavaScriptweb

2010-05-11 14:08:50

MySQL數(shù)字類型

2018-12-13 20:14:18

物聯(lián)網(wǎng)平臺物聯(lián)網(wǎng)IOT

2022-06-20 08:50:16

TypeScript類型語法

2009-11-24 18:15:37

博科資訊管理軟件

2009-08-03 17:41:20

ASP.NET Cac

2010-04-12 16:35:15

Oracle數(shù)據(jù)庫

2010-11-01 11:57:18

DB2客戶端

2013-09-02 15:35:00

2009-11-13 09:39:48

2009-07-16 16:23:59

Swing線程

2010-04-02 13:15:01

Oracle跟蹤

2009-09-25 15:58:04

Hibernate對象

2009-09-24 11:17:32

Hibernate查詢

2020-05-08 07:26:16

物聯(lián)網(wǎng)平臺物聯(lián)網(wǎng)IOT

2009-09-22 14:12:16

Hibernate S

2009-06-23 10:45:18

Hibernate支持

2009-05-07 15:02:42

OracleJoin查詢

2021-07-05 05:37:12

5G消息運(yùn)營商

2009-09-24 17:28:29

Hibernate S
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號