Why does JPA OneToOne (even OneToMany) have its orphanRemoval default to false

此生再无相见时 提交于 2020-12-15 05:20:49

问题


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?

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?

Thanks

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.

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

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.


回答1:


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.

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.



来源:https://stackoverflow.com/questions/64998743/why-does-jpa-onetoone-even-onetomany-have-its-orphanremoval-default-to-false

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!