JPA CascadeType.ALL不会删除孤儿 [英] JPA CascadeType.ALL does not delete orphans

查看:652
本文介绍了JPA CascadeType.ALL不会删除孤儿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  @OneToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER,mappedBy =所有者)
私人列表< Bikes>自行车;

我正在将数据库中的孤立角色挂起来。



我可以使用注释 org.hibernate.annotations.Cascade Hibernate特定的标签,但显然我不想将我的解决方案绑定到Hibernate执行。

编辑:看起来JPA 2.0会包含对此的支持。 解决方案

如果你在Hibernate中使用它,你必须显式地定义注释 CascadeType.DELETE_ORPHAN ,它可以用于与JPA CascadeType.ALL 结合使用。



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

执行顺序


  1. 获取主要行被删除
  2. 获取子元素

  3. 删除所有子元素

  4. 删除主要行

  5. / li>

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

  @OneToMany(mappedBy =foo,orphanRemoval = true)


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

@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.

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

解决方案

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.

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.

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

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

这篇关于JPA CascadeType.ALL不会删除孤儿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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