Hibernate中加载的三种类型

xiaoxiao2026-08-21  14

一:实体对象的加载

比如说:用户和订单的关系是一对多, 虽然它们有关联,但是默认是延迟加载 Lazy=“true”,最终得到的是代理对象,如果要访问代理对象的属性的话,则会抛出异常,

解决方法:left join fetch 迫切抓取连接

 

select u  from User u left join fetch Order o ;

 

 

二:普通属性

默认是:lazy=“false”

如果要改为延迟 方法较麻烦

 

三:集合对象

set list map  默认 Lazy =“true”

 

1:List

 

<list name="diarys" table="petDiary" cascade="all" inverse="true"> <key column="petId"></key> <index column="listindex"></index> <one-to-many class="com.lovo.po.PetDiary"/> </list>

 2:set

<set name="orders" table="t_order" cascade="all" inverse="true" lazy="true" > <key column="fk_customer_id"></key> <one-to-many class="Order"/> </set>

 3:map

private Map school=new HashMap(); public Map getSchool() { return school; } public void setSchool(Map school) { this.school=school; } private Map school=new HashMap(); public Map getSchool() { return school; } public void setSchool(Map school) { this.school=school; } Xml代码 <map name="school" table="schools"> <key column="pid" not-null="true" /> <map-key type="string" column="indet"/> <element type="float" column="score"/> </map>

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

转载请注明原文地址: https://www.6miu.com/read-5051602.html

最新回复(0)