在 Spring JSP 页面中使用集合时 Hibernate LazyInitializationException [英] Hibernate LazyInitializationException while using collection in Spring JSP page

查看:17
本文介绍了在 Spring JSP 页面中使用集合时 Hibernate LazyInitializationException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的实体:

@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
}

另一个:

@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
}

评估部分是从评估实体延迟加载的.我还有 spring 控制器方法,它是事务性的,并从数据库中加载评估部分:

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
}

一旦我将评估部分添加到弹簧模型图中,它们就可以在我的 JSP 页面中使用,并且我正在使用 JSTL 来遍历它们:

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>

从这个 JSP 页面抛出异常:

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

如果此集合已在事务中加载,这怎么可能?我只是想循环,此时 hibernate 不应该加载任何东西,因为它已经加载了.为什么会发生这种奇怪的事情?

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?

推荐答案

在视图中,entitymanager 已经关闭,因此集合中的元素无法检索那里的属性.您在控制器中编写的代码不会初始化集合中的元素(它是一个 LAZY 集合),而只是初始化了集合(而不是其中的元素).

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).

通过在 Web 配置中配置 OpenEntityManagerInViewFilter 强制实体管理器保持打开状态.

Either force the entitymanager to remain open by configuring the OpenEntityManagerInViewFilter in your web configuration.

或者更改您的控制器代码以包含对 Hibernate.initialize 的调用以正确初始化您的集合.

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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆