hibernate中主表和从表查询懒加载

解决懒加载的问题

Exception in thread "main" org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role

----主表

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.po.Sortp" table="sortp" >
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="assigned" />
</id>
<property name="name" type="java.lang.String">
<column name="name" length="40" not-null="true" />
</property>
<set name="productses" inverse="true" cascade="all" lazy="false">
<key>
<column name="sortid" not-null="true" />
</key>
<one-to-many class="com.po.Products" />
</set>
</class>
</hibernate-mapping>


---从表

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.po.Products" table="products" >
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="assigned" />
</id>
<many-to-one name="sortp" class="com.po.Sortp" fetch="select" cascade="all">
<column name="sortid" not-null="true" />
</many-to-one>
<property name="name" type="java.lang.String">
<column name="name" length="50" not-null="true" />
</property>
<property name="price" type="java.lang.Double">
<column name="price" precision="22" scale="0" not-null="true" />
</property>
<property name="saleprice" type="java.lang.Double">
<column name="saleprice" precision="22" scale="0" not-null="true" />
</property>
<property name="descript" type="java.lang.String">
<column name="descript" length="65535" not-null="true" />
</property>
<property name="contents" type="java.lang.String">
<column name="contents" length="65535" not-null="true" />
</property>
<property name="saledate" type="java.util.Date">
<column name="saledate" length="10" not-null="true" />
</property>
<property name="salecount" type="java.lang.Integer">
<column name="salecount" />
</property>
<property name="image" type="java.lang.String">
<column name="image" length="50" />
</property>
</class>
</hibernate-mapping>


在主表映射中需要设置懒加载,默认是为true 的
<set name="productses" inverse="true" cascade="all" lazy="true">


还有一种方式就是用代码处理

public static void main(String[] args) {
// TODO Auto-generated method stub
SortpDAO dao = new SortpDAO();
List<Sortp> list = dao.getAllSortp();

for(Sortp s :list){
if(!Hibernate.isInitialized(s.getProductses())){
Hibernate.initialize(s.getProductses());
}
}
dao.closeSession();
for(Sortp s :list){
System.out.println(s.getName());
Set<Products> set =s.getProductses();
Iterator<Products> it = set.iterator();
while(it.hasNext()){
System.out.println("-----------"+it.next().getDescript());
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值