解决懒加载的问题
Exception in thread "main" org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role
----主表
---从表
在主表映射中需要设置懒加载,默认是为true 的
还有一种方式就是用代码处理
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());
}
}
}