我有一个 bean,我在其中创建一个带有 Runnable 的新线程:
@Component
public class MyBean {
private final Task task = new Task();
@PersistenceContext
EntityManager em;
@PostConstruct
public void init() {
task.setEntityManager(em);
new Thread(task).start();
}
public static class Task implements Runnable {
@Setter
private EntityManager em;
@Override
public void run() {
while (true) {
// working with EntityManager
Thing t = em.findById(...); // Fetching a Thing from repo
t.getSomethingList(); // LazyInit exception
wait();
}
}
}
}
使用 init 方法,使用 EntityManager 的实例创建新的线程。当我尝试从存储库加载某些内容时,会话会立即关闭,并且获取任何惰性字段都会导致failed to lazily initialize a collection of role: Something, no session or session was closed
Hibernate 出现异常。
我尝试了所有 @Transactional 注释,但没有任何效果。我需要实现类似 OpenEntityManagerInView 的东西,但不同的是这不在视图之内。
谢谢
编辑1:
根据评论-我尝试使用
em.getTransaction().begin();
,但这让我很兴奋Not allowed to create transaction on shared EntityManager - use Spring transactions or EJB CMT
。skirsch 建议我应该在其他一些 bean 上调用 Transactional 方法。这就是我实际所做的 - 完全按照您的建议。我想让事情变得更简单,但我没有意识到其中的区别,所以我直接在课堂上演示了这个问题
Task
。总而言之,我完全像skirsch建议的那样,但问题仍然存在。