JPA OneToMany 关系未自动填充?

发布于 2024-12-17 13:44:57 字数 861 浏览 3 评论 0原文

我在 Task 和 TaskError 之间有一个简单的 OneToMany 关系。

任务映射任务错误:

@OneToMany(cascade = CascadeType.ALL, mappedBy = "task", orphanRemoval = true)
private List<TaskError> taskErrorCollection;

当任务被删除时,任务错误应该被删除,因此是orphanRemoval。

TaskError 连接到任务:

@JoinColumn(name = "task_id", referencedColumnName = "id")
@ManyToOne(optional = false, cascade = CascadeType.ALL)
private Task task;

当我使用以下代码添加 TaskError 时,任务中的任务错误列表不会更新:

TaskError taskError;
// set all taskErrors vars
taskErrorDAO.create(taskError);

因此数据库包含任务错误的一行,而任务中的任务错误列表未填充。显然,当我删除任务时,会出现错误: MySQLIntegrityConstraintViolationException:无法删除或更新父行

我个人是否需要填充任务中的任务错误列表?有没有办法让这个列表自动填充? taskErrorDAO.create(taskError) 之后的 em.flush 没有帮助。

关系

I have a simple OneToMany relation between Task and TaskError.

Task maps the TaskErrors:

@OneToMany(cascade = CascadeType.ALL, mappedBy = "task", orphanRemoval = true)
private List<TaskError> taskErrorCollection;

The TaskErrors should be deleted when the Task is deleted, hence the orphanRemoval.

TaskError is connected to a Task:

@JoinColumn(name = "task_id", referencedColumnName = "id")
@ManyToOne(optional = false, cascade = CascadeType.ALL)
private Task task;

The List of TaskErrors in Task is not updated when I add a TaskError with this code:

TaskError taskError;
// set all taskErrors vars
taskErrorDAO.create(taskError);

So the database contains a row for the TaskError, while the List of TaskErrors in Task is not populated. Obviously, when I delete the Task, it gives a error:
MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row

Am I personally required to populate the TaskError list in Task? Is there a way to have this List automatically be populated? A em.flush after taskErrorDAO.create(taskError) doesn't help.

relation

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

轻拂→两袖风尘 2024-12-24 13:44:57

开发人员负责实体关系的内存表示。所以 - 是的,您需要在 Task TaskError 列表中填充 TaskError 实例。

您可以尝试清理 TaskPersistenceContext 缓存并直接从数据库中重新获取它,例如:

em.refresh(task);

或者

em.clear();
task = em.find(Task.class, yourTaskId);

无论哪种方式,您的 JPA 提供程序都应该清除 in-实体的内存表示,点击数据库(正如您所说,已正确插入行)以获取它并正确加载列表。但是,从长远来看,我不会这样做,而只是相应地更新我的对象引用。
如果您拒绝更新实体的内存中关系,则无法使用 PersistenceContext 的缓存功能。为了获得正确的数据,在分配任何新的 TaskError 后,每次想要使用/获取 Task 实体时,您都需要访问数据库。

还有一件事 - 为什么 TaskError 中有 Cascade.ALL ?如果删除了 TaskError ,您是否要删除 Task 实例?

您还可以查看此资源了解更多信息。

The developer is responsible for in-memory representation of your entity relationships. So - yes, you are required to populate the TaskError instance in Task TaskError's list.

You can try cleaning up your PersistenceContext cache for Task and re-fetching it directly from the database, so something like:

em.refresh(task);

or

em.clear();
task = em.find(Task.class, yourTaskId);

Either way, your JPA provider should clear the in-memory representation of your Entity, hit the database (which, as you've said, has correctly inserted row) to fetch it and load the list properly. However, I wouldn't go this way in a long-term perspective but just update my objects references accordingly.
If you refuse to update in-memory relationships of your entities, than you cannot use caching features of PersistenceContext. In order to have correct data you will need to hit the database every time you want to use/fetch your Task entity after any new TaskError has been assigned to it.

One more thing - why do you have Cascade.ALL in TaskError? Do you want to remove Task instance if TaskError is removed?

You can also check this resource for more information.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文