为什么JPA OneToOne(甚至OneToMany)的orphanRemoval默认设置为false [英] Why does JPA OneToOne (even OneToMany) have its orphanRemoval default to false

查看:161
本文介绍了为什么JPA OneToOne(甚至OneToMany)的orphanRemoval默认设置为false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么JPA OneToOne (甚至 OneToMany orphanRemoval默认为false.

Why does JPA OneToOne (or even OneToMany for that matter) have its orphanRemoval default to false.

我的意思是,最好将删除操作强加给已从关系中删除的实体,并在默认情况下将删除操作级联到那些实体&用户选择不明确执行此操作吗?

I mean, wouldn't it be better to force the remove operation to entities that have been removed from the relationship and to cascade the remove operation to those entities by default & have the user make the choice to not do it explicitly?

如果默认值是false,当前行为是否不会导致孤立的条目(可能会变成僵尸记录),如果一个人仅使用默认值,那么这些条目会导致数据库表随时间膨胀?还是我在这里想念东西?

Would the current behavior of the default being false not result in orphan entries (that might potentially become zombie records) that might lead to bloat of the database tables over time if one just uses the defaults? Or am i missing things here?

谢谢

更新(12/6/2020): 我具体询问OneToOne&的原因是有原因的OneToMany.据我所知,在这两种情况下,删除的默认行为在大多数情况下是

UPDATE (12/6/2020): There is a reason i asked specifically about OneToOne & OneToMany. In both these cases atleast, as i understand, the default behavior of deleting is in most cases the most appropriate thing to do & not deleting is actually not recommended & hence the question.

已更新为官方oracle文档的链接上一点..同时还引用了以下部分以供快速参考,

Updated with link to official oracle documentation for the previous point..also quoting the section below for quick reference,

当一对一或一对多关系中的目标实体是 从关系中删除,通常需要级联 对目标实体的删除操作.这样的目标实体是 被视为"orphans",并且orphanRemoval属性可用于 指定应删除孤立的实体.

When a target entity in one-to-one or one-to-many relationship is removed from the relationship, it is often desirable to cascade the remove operation to the target entity. Such target entities are considered "orphans," and the orphanRemoval attribute can be used to specify that orphaned entities should be removed.

推荐答案

如果orphanRemoval默认操作为true,则它将在用户不知情的情况下删除关联的表记录.通常,大多数开发人员可能会忽略级联操作的默认行为,并最终删除关联的表数据.如果删除了数据,则无法取回数据. (我认为)想像是在没有用户知识的情况下删除数据,这有点吓人.

If the orphanRemoval default operation is true then it will delete the associated table records without user knowledge. It is common that most developers may overlook cascade operations default behavior and end up deleting the associated table data. If you have deleted the data and there is no way to get the data back. It is a bit scary (in my opinion) to imagine deleting the data without user knowledge.

如果您忽略了orphanRemoval标志并将其保留为默认选项,则当前行为将允许您选择该选项.您可以选择清理或保留孤儿记录.可以使用带有连接子句的简单删除查询来清理孤立记录.

The current behavior will let you choose the option if you have overlooked the orphanRemoval flag and left it to the default option. You have a choice to clean up the orphan records or leave it as it is. Cleaning up the orphan records can be done with a simple delete query with a join clause.

在某些情况下,您可能需要孤立记录来追溯历史记录.例如-订单和产品信息.可以删除产品,但您不想删除关联的订单.

There are scenarios where you might need orphan records to trace back the history. For example - Order and Product information. A product can be deleted but you don't want to remove the associated order.

这篇关于为什么JPA OneToOne(甚至OneToMany)的orphanRemoval默认设置为false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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