failed to lazily initialize a collection of role
程序员文章站
2022-04-11 16:51:28
...
因为有的时候我们使用JPA的时候由于某个实体的属性数据量比较大所以使用延迟加载来优化,而在struts2的action中使用到实体的时候却因为实体管理器已经关闭实体成为游离态,且数据延迟加载导致没有加载我们所需要的数据,比如:
@OneToMany(cascade = { CascadeType.REFRESH, CascadeType.REMOVE }, mappedBy = "parent")
public Set<ProductType> getChildTypes() {
return childTypes;
}
public void setChildTypes(Set<ProductType> childTypes) {
this.childTypes = childTypes;
}
这里使用到了延迟加载(默认方式),而实体管理器关闭之后我们在jsp中又要访问该数据:
${fn:length(type.childTypes)
这样会出现:failed to lazily initialize a collection of role我们可以通过在配置文件中添加(必须要在FilterDispatcher之前):
<!-- 解决延迟加载时EntityManager已经关闭而无法使用的问题,但是性能损耗(要放在FilterDispatcher前面) -->
<filter>
<filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
<!-- include this if you use hibernate -->
<!--
org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter
-->
<filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
如此问题解决。
推荐阅读
-
java 解决实体类序列化时,报failed to lazily initialize 的问题
-
解决问题:failed to lazily initialize a collection of role异常解决
-
JPA中failed to lazily initialize a collection of role:‘xxxx‘ no session or session was closed异常!
-
OneToMany字段序列化 failed to lazily initialize a collection of role
-
failed to lazily initialize a collection of role: , could not initialize proxy - no Session
-
Hibernate 延迟加载的错误 failed to lazily initialize a collection of role
-
failed to lazily initialize a collection of role
-
failed to lazily initialize a collection of role 异常
-
failed to lazily initialize a collection of role 的问题
-
SSH项目,failed to lazily initialize a collection of role