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 />
</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 />
</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 />
</id>
<many-to-one name="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="false">
还有一种方式就是用代码处理
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());
}
}
}
1 楼 lizhihui19871127 2012-05-08 楼主:和你探讨一下
一、我觉得你的第一种方式,不好。为什么呢,因为是一对多的关系,如果lazy设置为false的话,加载主对象的时候,会把所有的依赖关系的对象全部读取出来。你想想,如果是数百万个实例对象,系统性能如何?考虑过没有?所以一般都是多对一的时候才设置layz为true。
二、代码处理的话,也不对,如果是懒加载,都没有加载对应的关联对象,你的Set<Products> set =s.getProductses();
还能获取到值?应该是空的吧。
我也是今天研究了一下,欢迎一起研讨一下,一起进步~
不敬之处,敬请谅解~