lazy="false"的问题?
一张表Sort(类别),一张表errata(勘误)
Sort与errata多对一.一个类别下有多个勘误.
Sort的hbm文件是
...
<set name="erratas" inverse="true" lazy="false">
<key>
<column name="sort_id" not-null="true" />
</key>
<one-to-many class="com.daacc.dao.errata.Errata" />
</set>
...
Sort的pojo:
...
private Set erratas=new HashSet(0);
...
errata的hbm
...
<many-to-one name="sort"
class="com.daacc.dao.sort.Sort" fetch="select" lazy="false">
<column name="sort_id" not-null="true" unique="true" />
</many-to-one>
...
errata的pojo:
...
private Sort sort;
...
当我要查询所有种类时.
如果
<set name="erratas" inverse="true" lazy="false">
<key>
<column name="sort_id" not-null="true" />
</key>
<one-to-many class="com.daacc.dao.errata.Errata" />
</set>
加上lazy="false"
出现
Hibernate: select sort0_.id as id0_, sort0_.parent_id as parent2_2_0_, sort0_.name as name2_0_, sort0_.description as descript4_2_0_ from sort sort0_ where sort0_.id=?
Hibernate: select erratas0_.sort_id as sort3_1_, erratas0_.id as id1_, erratas0_.id as id0_, erratas0_.question_id as question2_8_0_, erratas0_.sort_id as sort3_8_0_, erratas0_.page as page8_0_, erratas0_.description as descript5_8_0_, erratas0_.source as source8_0_, erratas0_.correct as correct8_0_, erratas0_.date as date8_0_, erratas0_.state as state8_0_ from errata erratas0_ where erratas0_.sort_id=?
org.springframework.orm.hibernate3.HibernateSystemException: illegal access to loading collection; nested exception is org.hibernate.LazyInitializationException: illegal access to loading collection
org.hibernate.LazyInitializationException: illegal access to loading collection
的错误.
如果不加lazy="false" 能查出种类,但是我继续查种类的分类时,
我用这样的代码
Sort sort = (Sort) BusFacade.sortBus.getSort(Integer.parseInt(sortId));
Set set = sort.getErratas();
出现
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.daacc.dao.sort.Sort.erratas - no session or session was closed
的错误
如何配置多对一关系呢?