风人园

弱水三千,只取一瓢,便能解渴;佛法无边,奉行一法,便能得益。
随笔 - 99, 文章 - 181, 评论 - 56, 引用 - 0
数据加载中……

hibernate 延迟加载的错误 failed to lazily initialize a collection of role

这个问题一般出现在一对多的情况下,解决的方法有两种
1、设置lazy=false
如果是用annotation,则配置如下
@OneToMany(
   targetEntity = CourseAuthorizationItem.class,
   cascade = {CascadeType.PERSIST, CascadeType.MERGE},
   mappedBy = "course", fetch=FetchType.EAGER
  )
将fetch类型设置成直接获取

2、就是使用filter,过滤所有的链接
如果在使用filter的时候,要配置事务处理,否则会导致session处于只读状态而不能做修改、删除的动作

<web-app>

<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate.support.OpenSessionInViewFilter
</filter-class>
</filter>

<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>

</web-app>

posted on 2008-07-05 09:54 风人园 阅读(4791) 评论(1)  编辑  收藏 所属分类: Hibernate

评论

# re: hibernate 延迟加载的错误 failed to lazily initialize a collection of role  回复  更多评论   

恩,谢谢,不过
方法1不好,因为我不想每次都加载子表,很多时候不需要读取子表
方法2试过了,实际运行网站时可以用,但是在junit测试时还是报session关闭的错误,不知道测试时应该怎么解决?
2009-06-02 11:36 | shappy

只有注册用户登录后才能发表评论。


网站导航: