JPA CascadeType.ALL不会删除孤儿 [英] JPA CascadeType.ALL does not delete orphans
问题描述
我在使用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>
执行顺序
- 获取主要行被删除
- 获取子元素
- 删除所有子元素
- 删除主要行
- / 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
- fetch main row to be deleted
- fetch child elements
- delete all child elements
- delete main row
- close session
With JPA 2.0, you can now use the option orphanRemoval = true
@OneToMany(mappedBy="foo", orphanRemoval=true)
这篇关于JPA CascadeType.ALL不会删除孤儿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!