为什么我在一个 Set 上使用 FetchType.LAZY 而在另一个 Set 上没有得到 LazyInitializationException?

Why am I getting a LazyInitializationException when using FetchType.LAZY on one Set, but not on the other?

我 运行 遇到了一个很奇怪的问题;似乎有些 Set 会延迟加载,但其他的不会,但底层 class 布局完全相同:

我有 4 个 classes,在 Spring-boot Hibernate 映射中:

这些是顶级class,事件

@Entity
public class Event {

    // Getters, setters, etc. are omitted.

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "event", cascade = CascadeType.ALL)
    @ElementCollection(targetClass = Question.class)
    private Set<Question> questions = new HashSet<>();

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "event", cascade = CascadeType.ALL)
    @ElementCollection(targetClass = Participant.class)
    private Set<Participant> participants = new HashSet<>();

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "event", cascade = CascadeType.ALL)
    @ElementCollection(targetClass = Run.class)
    private Set<Run> runs = new HashSet<>();

}

这些是链接到这个事件根节点的 3 个 classes class:

问题:

@Entity
public class Question {

    // Getters, setters, etc. are omitted.

    @ManyToOne(fetch = FetchType.LAZY)
    private Event event;
}

参与者:

@Entity
public class Participant {

    // Getters, setters, etc. are omitted.

    @ManyToOne(fetch = FetchType.LAZY)
    private Event event;
}

运行:

@Entity
public class Run {

    // Getters, setters, etc. are omitted.

    @ManyToOne(fetch = FetchType.LAZY)
    private Event event;
}

当然,还有与 JPA 相关的空构造函数、其他构造函数、大量 Hibernate/JPA 标记和更多底层关系,但我相信这些在这里没有什么区别。将 Set<Run> 变量引入 Event class.

后开始出现问题

运行ning 程序,并创建一个 Event 类型的对象,添加事件,一切正常,但创建一个新的 运行 对象并尝试将其添加到 Event.runs 字段在 JUnit 测试中出现以下错误:

正在呼叫 event.getRuns().add(<a new 'Run' object>)

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.ps.model.orm.Event.runs, could not initialize proxy - no Session
at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:576)
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:215)
at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:555)
at org.hibernate.collection.internal.PersistentSet.add(PersistentSet.java:202)
at com.ps.HibernateTestApplicationTests.testRunCreation(HibernateTestApplicationTests.java:88)
at com.ps.HibernateTestApplicationTests.testAllTheThings(HibernateTestApplicationTests.java:37)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:254)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)
at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=15=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:193)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

在这一点上,惰性实例化对我来说似乎是错误的。当我将 Event.runs@OneToMany 标记的 FetchType.LAZY 更改为 FetchType.EAGER 时,程序按预期执行,但我不能我一辈子都弄不明白为什么 LAZY 在这里不起作用,而在其他 classes 却起作用,因为它们的结构几乎相同。

其他的工作,LAZY EAGER,而这个没有,我真的没有看到映射或这里的任何东西,所以即使我现在只使用 EAGER 类型,而不是 LAZY,我知道这是不鼓励的,并且想知道是否有任何事情我可能已经看过了。

堆栈跟踪告诉您问题 -

could not initialize proxy - no Session

只需确保在 Session 仍然可用的事务中调用 event.getRuns().add(<a new 'Run' object>)
如果使用 Spring 只需用 @Transactional

注释该方法

像这样

@Transactional
public void doSomething(){

Event event= // get Event from DB ;
event.getRuns.add(//new Run);
//some other stuff
}

我有同样的 problem.I 解决了这个问题,将以下代码添加到我的 web.xml file.I 希望它能帮助你 ;)

<filter>
    <filter-name>SpringOpenEntityManagerInViewFilter</filter-name>
    <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>SpringOpenEntityManagerInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>