分离的实体和延迟加载字段 - 工作线程 [英] Detached entity and lazy loaded fields - worker threads

查看:82
本文介绍了分离的实体和延迟加载字段 - 工作线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到以下问题。类A从数据库中提取一些对象,这些对象本身是可运行的,并在线程池中运行它们。他们需要在运行时更新其状态(他们更新在其上注册的监听器)。问题是他们在实体处于分离状态时访问延迟加载的字段(因为事务只包含主线程) - 所以当然也有例外(com.sun.jdi.InvocationException发生调用方法)。我猜每个这些实体的注释运行方法将无法正常工作,因为实体已被加载到不同的事务中,对吗?如何应对这种情况?

I've got the following problem. Class A is retreiving some objects from the database which are runnables themselves and runs them in a thread pool. They need to update their state as they run (they update listeners registered on them). The problem is they access lazy-loaded fields while the entity is in detached state (as transaction covers only the main thread) - so of course there are exceptions (com.sun.jdi.InvocationException occurred invoking method.). I guess annotating run method of each of those entities won't work, because the entity is already loaded in different transaction am I right? How to cope with this situation?

谢谢!

Thank you!

推荐答案

无法访问延迟加载在分离的实体中的字段,您需要首先附加实体。

It's not possible to access lazy loaded fields in a detached entity, you need to attach the entity first.

通过调用 session.merge()或entityManager.merge()。

Attach the entity to a session by calling session.merge() or entityManager.merge() before trying to access the lazy loaded fields.

这篇关于分离的实体和延迟加载字段 - 工作线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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