4

我有这样的实体:

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

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

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

一旦我将评估部分添加到 spring 模型图中,它们就会在我的 JSP 页面中可用,并且我正在使用 JSTL 循环它们:

<c:forEach var="assessmentPart" items="${assessmentParts}">
     //Not loading any lazy stuff, just getting an ID of assessment part
</c:forEach>

从此 JSP 页面抛出异常:

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: my.package.something.Assessment.assessmentParts, could not initialize proxy - no Session

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

4

3 回答 3

8

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

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

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

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

要么创建一个自定义查询,强制急切获取集合。

于 2013-09-27T10:38:52.940 回答
2

改变

model.put("assessmentParts", assessment.getAssessmentParts());

Hibernate.initialize(assessment.getAssessmentParts());

初始化您的收藏。

方法调用assessment.getAssessmentParts()未初始化您的集合。此方法调用只会返回一个集合包装器,并且您需要将此包装器传递给Hibernate.initialize手动初始化的方法。

编辑:

使用 JPA,您可以使用 JPQL Fetch Joins 来获取(在需要AssessmentParts时),覆盖默认行为:Assessment

SELECT a FROM Assessment asmt LEFT JOIN FETCH asmt.assessmentParts WHERE asmt.id = :id
于 2013-09-27T10:34:25.720 回答
0

从 EntityManager 加载 AssessmentPart 将加载评估@ManyToOne(fetch = FetchType.EAGER)

AssessmentPart assessmentPart = //laods a part with entity manager
Assessment assessment = assessmentPart.getAssessment();

尝试从评估中提取AssessmentParts 将不起作用,因为它不是@OneToMany(fetch = FetchType.LAZY)并且会话已经关闭

model.put("assessmentParts", assessment.getAssessmentParts());

以新方法从 EntityManager 通过评估获取评估部件列表应该可以解决问题。

于 2013-09-27T10:27:49.333 回答