Java JPA CascadeType.ALL 不删除孤儿

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/306144/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 12:55:04  来源:igfitidea点击:

JPA CascadeType.ALL does not delete orphans

javahibernateormjpajpa-2.0

提问by Paul Whelan

I am having trouble deleting orphan nodes using JPA with the following mapping

我在使用 JPA 和以下映射删除孤立节点时遇到问题

@OneToMany (cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "owner")
private List<Bikes> bikes;

I am having the issue of the orphaned roles hanging around the database.

我遇到了围绕数据库的孤立角色的问题。

I can use the annotation org.hibernate.annotations.CascadeHibernate specific tag but obviously I don't want to tie my solution into a Hibernate implementation.

我可以使用注释org.hibernate.annotations.CascadeHibernate 特定标记,但显然我不想将我的解决方案与 Hibernate 实现联系起来。

EDIT: It seems JPA 2.0 will include support for this.

编辑:看来 JPA 2.0 将包含对此的支持。

采纳答案by Varun Mehta

If you are using it with Hibernate, you'll have to explicitly define the annotation CascadeType.DELETE_ORPHAN, which can be used in conjunction with JPA CascadeType.ALL.

如果将它与 Hibernate 一起使用,则必须明确定义 annotation CascadeType.DELETE_ORPHAN,它可以与 JPA 结合使用CascadeType.ALL

If you don't plan to use Hibernate, you'll have to explicitly first delete the child elements and then delete the main record to avoid any orphan records.

如果您不打算使用 Hibernate,则必须首先明确删除子元素,然后删除主记录以避免任何孤立记录。

execution sequence

执行顺序

  1. fetch main row to be deleted
  2. fetch child elements
  3. delete all child elements
  4. delete main row
  5. close session
  1. 获取要删除的主行
  2. 获取子元素
  3. 删除所有子元素
  4. 删除主行
  5. 闭会

With JPA 2.0, you can now use the option orphanRemoval = true

使用 JPA 2.0,您现在可以使用选项orphanRemoval = true

@OneToMany(mappedBy="foo", orphanRemoval=true)

回答by toolkit

According to Java Persistence with Hibernate, cascade orphan deleteis not available as a JPA annotation.

根据Java Persistence with Hibernate级联孤立删除不能用作 JPA 注释。

It is also not supported in JPA XML.

JPA XML 也不支持它。

回答by u?6??n? ???u?p

If you are using JPA with EclipseLink, you'll have to set the @PrivateOwnedannotation.

如果您将 JPA 与 EclipseLink 一起使用,则必须设置@PrivateOwned注释。

Documentation: Eclipse Wiki - Using EclipseLink JPA Extensions - Chapter 1.4 How to Use the @PrivateOwned Annotation

文档:Eclipse Wiki - 使用 EclipseLink JPA 扩展 - 第 1.4 章如何使用 @PrivateOwned 注释

回答by Kango_V

If you are using JPA 2.0, you can now use the orphanRemoval=trueattribute of the @xxxToManyannotation to remove orphans.

如果您使用的是 JPA 2.0,您现在可以使用注释的orphanRemoval=true属性@xxxToMany来移除孤立项。

Actually, CascadeType.DELETE_ORPHANhas been deprecated in 3.5.2-Final.

实际上,CascadeType.DELETE_ORPHAN已在 3.5.2-Final 中弃用。

回答by Valéry Stroeder

I just find this solution but in my case it doesn't work:

我只是找到了这个解决方案,但在我的情况下它不起作用:

@OneToMany(cascade = CascadeType.ALL, targetEntity = MyClass.class, mappedBy = "xxx", fetch = FetchType.LAZY, orphanRemoval = true) 

orphanRemoval = truehas no effect.

orphanRemoval = true没有效果。

回答by Kohan95

Just @OneToMany(cascade = CascadeType.ALL, mappedBy = "xxx", fetch = FetchType.LAZY, orphanRemoval = true).

只是@OneToMany(cascade = CascadeType.ALL, mappedBy = "xxx", fetch = FetchType.LAZY, orphanRemoval = true)

Remove targetEntity = MyClass.class, it works great.

删除targetEntity = MyClass.class,效果很好。

回答by Simone Gianni

For the records, in OpenJPA before JPA2 it was @ElementDependant.

根据记录,在 JPA2 之前的 OpenJPA 中,它是 @ElementDependant。

回答by reshma

you can use @PrivateOwned to delete orphans e.g

您可以使用@PrivateOwned 删除孤儿,例如

@OneToMany(mappedBy = "masterData", cascade = {
        CascadeType.ALL })
@PrivateOwned
private List<Data> dataList;

回答by Sergii Shevchyk

╔═════════════╦═════════════════════╦═════════════════════╗
║   Action    ║  orphanRemoval=true ║   CascadeType.ALL   ║
╠═════════════╬═════════════════════╬═════════════════════╣
║   delete    ║     deletes parent  ║    deletes parent   ║
║   parent    ║     and orphans     ║    and orphans      ║
╠═════════════╬═════════════════════╬═════════════════════╣
║   change    ║                     ║                     ║
║  children   ║   deletes orphans   ║      nothing        ║
║    list     ║                     ║                     ║
╚═════════════╩═════════════════════╩═════════════════════╝

回答by Bevor

I had the same problem and I wondered why this condition below did not delete the orphans. The list of dishes were not deleted in Hibernate (5.0.3.Final) when I executed a named delete query:

我有同样的问题,我想知道为什么下面的这个条件没有删除孤儿。当我执行命名删除查询时,在 Hibernate (5.0.3.Final) 中没有删除菜肴列表:

@OneToMany(mappedBy = "menuPlan", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Dish> dishes = new ArrayList<>();

Then I remembered that I must not use a named delete query, but the EntityManager. As I used the EntityManager.find(...)method to fetch the entity and then EntityManager.remove(...)to delete it, the dishes were deleted as well.

然后我想起我不能使用命名删除查询,而是使用 EntityManager。当我使用该EntityManager.find(...)方法获取实体然后EntityManager.remove(...)删除它时,菜肴也被删除了。