lazy="false"的问题?

suncheng_hong 2007-07-10 09:24:29
一张表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
的错误
如何配置多对一关系呢?


...全文
422 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
suncheng_hong 2007-07-10
  • 打赏
  • 举报
回复
谢谢.
zxh2208180 2007-07-10
  • 打赏
  • 举报
回复
Set erratas=new HashSet(0);把0去掉.
文档我也不知道哪有,自己搜索下吧
suncheng_hong 2007-07-10
  • 打赏
  • 举报
回复
九天,哪里有hbm属性的文档.在官方的没找到.
hashCode方法中包含了及联属性所致.
zxh2208180 2007-07-10
  • 打赏
  • 举报
回复
Sort的hbm文件的lazy="false"保留,inverse="true"改成false,增加cascade="all-delete-orphan"和outer-join="true";
把errata的hbm文件的lazy="false"去掉;增加cascade="save-update" outer-join="auto"
suncheng_hong 2007-07-10
  • 打赏
  • 举报
回复
如果去掉的话.种类能查到.
但是我想查种类的勘误,
用这样的代码
Sort sort = (Sort) BusFacade.sortBus.getSort(Integer.parseInt(sortId));
Set set = sort.getErratas();
Iterator it = set.iterator();
List errList = new ArrayList();
while (it.hasNext()) {
errList.add(it.next());
}
request.setAttribute("errList", errList);

这样也会报错.
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.daacc.dao.sort.Sort.erratas - no session or session was closed
zxh2208180 2007-07-10
  • 打赏
  • 举报
回复
你把errata的hbm文件的lazy="false"去掉就好了

67,541

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧