@Entity public class Organization {
@OneToOne(fetch = FetchType.EAGER)
@OnDelete(action = OnDeleteAction.CASCADE)
@Cascade(value = DELETE_ORPHAN)
private Days days;
}
我有以下实体定义,它生成一个sql,在父对象被删除时对@OneToOne条目进行级联删除.但是删除组织时不会删除“days”条目.
最佳答案
My query looks like this “delete from Organization where some_key_id = ?” (am not deleting this based on primary key id)
批量删除(你应该在你的问题中提到)不会级联到任何东西.引用JPA 1.0规范:
4.10 Bulk Update and Delete Operations
…
A delete operation only applies to
entities of the specified class and
its subclasses. It does not cascade to
related entities.
这是一个非常恼人的限制,并且有许多RFE可以改进(HHH-695,HHH-1917,HHH-3337,HHH-5529等).
>自己清理儿童桌
>在架构中使用级联外键.
现在奇怪的部分……我对@OnDelete(action = OnDeleteAction.CASCADE)的理解是,这个注释应该用于确保使用适当的ON DELETE CASCADE子句创建外键(解决方案#2).换句话说,我希望事情能发挥作用.
Hibernate是否生成了Organization表?你能看看DDL吗?你看到了预期的ON DELETE CASCADE吗?如果没有,请添加它.