简体   繁体   English

JPA CascadeType.ALL 不删除孤儿

[英]JPA CascadeType.ALL does not delete orphans

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.Cascade Hibernate specific tag but obviously I don't want to tie my solution into a Hibernate implementation.我可以使用注释org.hibernate.annotations.Cascade Hibernate 特定标记,但显然我不想将我的解决方案与 Hibernate 实现联系起来。

EDIT : It seems JPA 2.0 will include support for this.编辑:看来 JPA 2.0 将包含对此的支持。

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 一起使用,则必须明确定义注释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闭会

With JPA 2.0, you can now use the option orphanRemoval = true使用 JPA 2.0,您现在可以使用选项orphanRemoval = true

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

If you are using JPA 2.0, you can now use the orphanRemoval=true attribute of the @xxxToMany annotation to remove orphans.如果您使用的是 JPA 2.0,您现在可以使用@xxxToMany注释的orphanRemoval=true属性来删除孤儿。

Actually, CascadeType.DELETE_ORPHAN has been deprecated in 3.5.2-Final.实际上, CascadeType.DELETE_ORPHAN在 3.5.2-Final 中已被弃用。

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

If you are using JPA with EclipseLink, you'll have to set the @PrivateOwned annotation.如果您将 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 注释

you can use @PrivateOwned to delete orphans eg您可以使用@PrivateOwned 删除孤儿,例如

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

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 = true has no effect. orphanRemoval = true没有效果。

According to Java Persistence with Hibernate , cascade orphan delete is not available as a JPA annotation.根据Java Persistence with Hibernate级联孤立删除不能用作 JPA 注释。

It is also not supported in JPA XML. JPA XML 也不支持它。

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.然后我想起我不能使用命名删除查询,而是使用 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.find(...)方法获取实体,然后使用EntityManager.remove(...)删除它时,菜肴也被删除了。

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 ,效果很好。

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

I was using one to one mapping , but child was not getting deleted JPA was giving foreign key violation我正在使用一对一映射,但孩子没有被删除 JPA 导致外键违规

After using orphanRemoval = true , issue got resolved使用 orphanRemoval = true 后,问题得到解决

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM