在Spring JSP页面中使用集合时发生Hibernate LazyInitializationException [英] Hibernate LazyInitializationException while using collection in Spring JSP page
问题描述
@Entity
@Table(name =ASSESSMENT)
public class Assessment {
//所有其他字段..
@OneToMany(fetch = FetchType.LAZY,cascade = CascadeType.ALL,mappedBy =assessment)
@OrderBy(value =order ASC)
private List< AssessmentPart> assessmentParts = new LinkedList<>();
公共列表< AssessmentPart> getAssessmentParts(){
return assessmentParts;
}
//所有其他获取者/设置者
}
另一个:
$ p $ @Entity
@Table(name =ASSESSMENT_PART)
公共类AssessmentPart {
//所有其他字段
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name =ASSESSMENT_ID,nullable =假)
私人评估评估;
公众评估getAssessment(){
回报评估;
}
public void setAssessment(评估评估){
this.assessment =评估;
}
//所有其他获取者/设置者
}
评估部分从评估实体延迟加载。我也有Spring的控制器方法,它是Transactional并从数据库中加载评估部分:
$ $ $ $ $ $ $ $ $ $ $ public void doSomething (String partId,Map< String,Object> model){
AssessmentPart assessmentPart = //载入实体管理器的一部分
评估评估= assessmentPart.getAssessment(); //获得评估
model.put(assessmentParts,assessment.getAssessmentParts()); //将所有评估部分添加到弹簧模型图中
}
一旦我添加了评估部分到Spring模型图中,它们在我的JSP页面中可用,并且我使用JSTL来循环它们:
< c :forEach var =assessmentPartitems =$ {assessmentParts}>
//不加载任何懒惰的东西,只是获得评估部分的ID
< / c:forEach>
从此JSP页面抛出异常:
org.hibernate.LazyInitializationException:未能延迟初始化角色集合:my.package.something.Assessment.assessmentParts,无法初始化代理 - 无Session b $ b
如果这个集合已经被加载到一个事务中,这是如何实现的?我只是试图循环,hibernate现在不应该加载任何东西,因为它已经加载。为什么这种奇怪的事情发生?
在视图中,entitymanager已经关闭,因此集合中的元素无法检索那里的属性。您在控制器中编写的代码不会初始化集合中的元素(这是一个LAZY集合),但只有集合被初始化(而不是集合中的元素)。
<通过在你的web配置中配置
OpenEntityManagerInViewFilter
来强制entitymanager保持打开。 或者更改你的控制器代码包含对 Hibernate.initialize
的调用,以正确地初始化您的集合。
@Transactional
public void doSomething(String partId,Map< String,Object> model){
AssessmentPart assessmentPart = //嵌入实体管理器
评估评估= assessmentPart.getAssessment(); //获得评估
Hibernate.initialize(assesment.getAssesmentParts()); //初始集合
model.put(assessmentParts,assessment.getAssessmentParts()); //将所有评估部分添加到Spring模型映射中
}
或者创建一个自定义查询哪些强制提取集合。
I have entities like this:
@Entity
@Table(name = "ASSESSMENT")
public class Assessment {
//All other fields..
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "assessment")
@OrderBy(value = "order ASC")
private List<AssessmentPart> assessmentParts = new LinkedList<>();
public List<AssessmentPart> getAssessmentParts() {
return assessmentParts;
}
//All other getters/setters
}
The other one:
@Entity
@Table(name = "ASSESSMENT_PART")
public class AssessmentPart {
//All other fields
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "ASSESSMENT_ID", nullable = false)
private Assessment assessment;
public Assessment getAssessment() {
return assessment;
}
public void setAssessment(Assessment assessment) {
this.assessment = assessment;
}
//All other getters/setters
}
Assessment parts are lazy loaded from assessment entity. I also have spring controller method which is Transactional and loads assessment part from the database:
@Transactional
public void doSomething(String partId, Map<String, Object> model) {
AssessmentPart assessmentPart = //laods a part with entity manager
Assessment assessment = assessmentPart.getAssessment(); //Getting the assessments
model.put("assessmentParts", assessment.getAssessmentParts()); //adding all assessments parts into spring model map
}
Once I've added assessment parts into spring model map, they become available in my JSP page and I am using JSTL to loop through them:
<c:forEach var="assessmentPart" items="${assessmentParts}">
//Not loading any lazy stuff, just getting an ID of assessment part
</c:forEach>
The exception is thrown from this JSP page:
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: my.package.something.Assessment.assessmentParts, could not initialize proxy - no Session
How is this possible if this collection has already been loaded in a transaction? I am just trying to loop through, hibernate shouldn't be loading anything at this point, because it was already loaded. Why is this weird thing happening?
In the view the entitymanager is already closed and as such the elements in your collections fail to retrieve there properties. The code you wrote in the controller doesn't initialize the elements in the collection (it is a LAZY collection) but only the collection is initialized (not the elements inside it).
Either force the entitymanager to remain open by configuring the OpenEntityManagerInViewFilter
in your web configuration.
Or change your controller code to include a call to Hibernate.initialize
to properly initialize your collection.
@Transactional
public void doSomething(String partId, Map<String, Object> model) {
AssessmentPart assessmentPart = //laods a part with entity manager
Assessment assessment = assessmentPart.getAssessment(); //Getting the assessments
Hibernate.initialize(assesment.getAssesmentParts()); // Init collection
model.put("assessmentParts", assessment.getAssessmentParts()); //adding all assessments parts into spring model map
}
Either that or create a custom query which forces eager fetching of the collection.
这篇关于在Spring JSP页面中使用集合时发生Hibernate LazyInitializationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!