JPA OneToMany 关系未自动填充?
我在 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
开发人员负责实体关系的内存表示。所以 - 是的,您需要在
Task
TaskError
列表中填充TaskError
实例。您可以尝试清理
Task
的PersistenceContext
缓存并直接从数据库中重新获取它,例如:或者
无论哪种方式,您的 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 inTask
TaskError
's list.You can try cleaning up your
PersistenceContext
cache forTask
and re-fetching it directly from the database, so something like:or
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 yourTask
entity after any newTaskError
has been assigned to it.One more thing - why do you have
Cascade.ALL
inTaskError
? Do you want to removeTask
instance ifTaskError
is removed?You can also check this resource for more information.