这篇文章主要讲解了“Hibernate中加载的类型有几种”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Hibernate中加载的类型有几种”吧!
一:Hibernate实体对象的加载
比如说:用户和订单的关系是一对多,虽然它们有关联,但是默认是延迟加载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中加载的类型有几种”的内容了,经过本文的学习后,相信大家对Hibernate中加载的类型有几种这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。