hibernate 中fetch=FetchType.LAZY问题
来源:互联网 发布:ucloud域名备案 编辑:程序博客网 时间:2024/04/28 09:33
我的实体类Deptment 中
@OneToMany(mappedBy="deptment",cascade=CascadeType.ALL,fetch=FetchType.LAZY)
public Set<Employee> getEmployees() {
return employees;
}
在dao层中
public List<Deptment> queryDeptList() {
List<Deptment> deptList = this.hibernateTemplate.find("from Deptment");
return deptList;
}
为什么在jsp中读取DeptmentList会报错:failed to lazily initialize a collection of role: model.Deptment.employees, no session or session was closed
public Set<Employee> getEmployees() {
return employees;
}
在dao层中
public List<Deptment> queryDeptList() {
List<Deptment> deptList = this.hibernateTemplate.find("from Deptment");
return deptList;
}
为什么在jsp中读取DeptmentList会报错:failed to lazily initialize a collection of role: model.Deptment.employees, no session or session was closed
这个是很典型的有懒加载引起的问题,你懒加载的时候只加载了了department的基本信息,而部门下的员工信息是懒加载的,当你要去用department下的employee信息,那么又会去加载。而此时你的session已经关闭了,所以才会报nosession错。
解决办法:在你的web.xml中添加下面的配置
<!-- 把session的关闭延迟到jsp页面显示之后,在配在struts2上面。-->
<filter>
<filter-name>OpenSessionInView</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInView</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
解决办法:在你的web.xml中添加下面的配置
<!-- 把session的关闭延迟到jsp页面显示之后,在配在struts2上面。-->
<filter>
<filter-name>OpenSessionInView</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInView</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
- hibernate 中fetch=FetchType.LAZY问题
- hibernate 配置@ManyToOne(cascade=CascadeType.ALL,fetch=FetchType.LAZY) 主导方问题删除问题。
- @ManyToOne(fetch=FetchType.LAZY)
- fetch = FetchType.LAZY 时,在代码中即使加载
- fetch = FetchType.LAZY懒加载
- hibernate 双向 OneToOne fetchType lazy 问题
- @Basic(fetch=FetchType.EAGER)和@Basic(fetch=FetchType.LAZY)
- hibrenate @ManyToOne(fetch = FetchType.EAGER) 和 lazy 区别
- hibrenate @ManyToOne(fetch = FetchType.EAGER) 和 lazy 区别
- hibrenate @ManyToOne(fetch = FetchType.EAGER) 和 lazy 区别
- 从Hibernate源码看@NotFound(action=NotFoundAction.IGNORE) 会导致Eager加载 @ManyToOne(fetch = FetchType.LAZY)无效
- fetch = FetchType.EAGER
- fetch = FetchType.EAGER
- Hibernate中lazy,inverse,cascade,fetch
- hibernate 中lazy、banth 、fetch的对比
- Hibernate中fetch和lazy介绍
- Hibernate中fetch和lazy介绍
- Hibernate中fetch和lazy介绍
- 开发中常见问题集锦
- Codeforces_357B_Flag Day(贪心)
- Extjs menu菜单怎么用
- 软件测试技能鉴定(模拟题)
- 安卓怎么限制横屏和竖屏
- hibernate 中fetch=FetchType.LAZY问题
- Android C2DM学习——云端推送(转载)
- Java:String和Date、Timestamp之间的转换
- 软件测试技能鉴定(模拟题)___含答案
- Poj 1650 & Zoj 1601 Integer Approximation (追赶法,分数逼近实数)
- Lync 2010升级到Lync 2013 之边缘服务器的部署!
- 网络子系统57_路由Scope
- pl/sql 游标变量实例
- Cocos2D-X定时器selector的使用