为什么在一个Set上使用FetchType.LAZY时会出现LazyInitializationException,而在另一个Set上没有?

时间:2016-05-29 23:44:12

标签: java spring hibernate spring-boot lazy-initialization

我在这里碰到了一个特殊的问题;好像有些套装会懒得加载,但其他套装不会,但基础类布局完全相同:

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

这些是顶级课程,活动

@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<>();

}

这三个链接到这个事件根节点类的类:

问题

@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>变量引入事件类时,问题就开始出现了。

运行程序,并创建类型为事件的对象添加事件,一切顺利,但创建新的运行对象并尝试将其添加到{{ 1}}字段在JUnit测试中给出了以下错误:

致电Event.runs

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$1.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$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.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) @OneToMany 标记的FetchType.LAZY更改为Event.runs时,程序按预期执行,但我无法执行为了我的生活,弄清楚为什么LAZY不在这里工作,而在其他班级,因为他们的结构几乎相同。

其他人工作,FetchType.EAGER LAZY,而这个没有,我真的没有看到映射或其他任何地方的区别,所以即使我现在只使用EAGER类型,而不是EAGER,我知道这是不鼓励的,并且想知道是否有任何事情我可能会看到结束了。

2 个答案:

答案 0 :(得分:2)

stacktrace告诉你问题 -

  

无法初始化代理 - 没有会话

确保在会话仍然可用的交易中调用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
}

答案 1 :(得分:0)

我遇到了同样的问题。我已经解决了将以下代码添加到我的 web.xml 文件中的问题。我希望它能为您提供帮助;)

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