问题描述
我在使用 JPA 和以下映射删除孤立节点时遇到问题
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.
我可以使用注释 org.hibernate.annotations.Cascade
Hibernate 特定标记,但显然我不想将我的解决方案与 Hibernate 实现联系起来.
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.
编辑:似乎 JPA 2.0 将包含对此的支持.
EDIT: It seems JPA 2.0 will include support for this.
推荐答案
如果您将它与 Hibernate 一起使用,则必须显式定义注释 CascadeType.DELETE_ORPHAN
,它可以用于与 JPA CascadeType.ALL
结合使用.
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,则必须先明确删除子元素,然后再删除主记录以避免任何孤立记录.
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.
执行顺序
- 获取要删除的主行
- 获取子元素
- 删除所有子元素
- 删除主行
- 闭会
在 JPA 2.0 中,您现在可以使用选项 orphanRemoval = true
With JPA 2.0, you can now use the option orphanRemoval = true
@OneToMany(mappedBy="foo", orphanRemoval=true)
这篇关于JPA CascadeType.ALL 不删除孤儿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!