无法级联删除@OneToOne成员

时间:2021-06-09 03:39:49
@Entity public class Organization {

    @OneToOne(fetch = FetchType.EAGER)
    @OnDelete(action = OnDeleteAction.CASCADE)
    @Cascade(value = DELETE_ORPHAN)
    private Days days;

}

I have the following entity definition and it generates a SQL to do a cascade delete on the @OneToOne entry when the parent object gets deleted. But it's not removing the "days" entry while deleting an Organization.

我有以下实体定义,它会生成一个SQL,以便在删除父对象时对@OneToOne条目执行级联删除。但是在删除组织时不会删除“days”条目。

This happens with h2, mysql databases, what could be the problem here.

这发生在h2,mysql数据库中,这可能是什么问题。

2 个解决方案

#1


5  

My query looks like this "delete from Organization where some_key_id = ?" (am not deleting this based on primary key id)

我的查询看起来像这样“从组织中删除some_key_id =?” (我不是根据主键ID删除它)

A bulk delete (you should mention that in your question) doesn't cascade to anything. Quoting the JPA 1.0 specification:

批量删除(你应该在你的问题中提到)不会级联到任何东西。引用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.

删除操作仅适用于指定类及其子类的实体。它不会级联到相关实体。

This is a very annoying limitation and there are many RFEs to improve things (HHH-695, HHH-1917, HHH-3337, HHH-5529, etc).

这是一个非常烦人的限制,有很多RFE可以改善(HHH-695,HHH-1917,HHH-3337,HHH-5529等)。

For now, possible solutions include:

目前,可能的解决方案包括:

  1. clean up the child table yourself
  2. 自己清理孩子桌子
  3. use cascading foreign keys in the schema.
  4. 在架构中使用级联外键。

Now the weird part... My understanding of @OnDelete(action = OnDeleteAction.CASCADE) is that this annotation is supposed to be used to ensure that the foreign key is created with the appropriate ON DELETE CASCADE clause (solution #2). In other words, I'd expect things to work.

现在奇怪的部分......我对@OnDelete(action = OnDeleteAction.CASCADE)的理解是,这个注释应该用于确保使用适当的ON DELETE CASCADE子句创建外键(解决方案#2)。换句话说,我希望事情能发挥作用。

Did Hibernate generate the Organization table? Can you check the DDL? Do you see the expected ON DELETE CASCADE? If not, add it.

Hibernate是否生成了Organization表?你能看看DDL吗?你看到了预期的ON DELETE CASCADE吗?如果没有,请添加它。

#2


6  

Well, I guess you should add a

好吧,我想你应该添加一个

@Cascade(value = {DELETE, DELETE_ORPHAN})

Note that in JPA 2.0 you don't have to use the hibernate-sepcific @Cascade annotation - @*ToMany has an option to delete orphans.

请注意,在JPA 2.0中,您不必使用hibernate-sepcific @Cascade注释 - @ * ToMany可以选择删除孤儿。

Update: when using queries cascades are not handled. You have to handle them manually. This is expected and documented behaviour.

更新:使用查询时,不会处理级联。你必须手动处理它们。这是预期和记录的行为。

#1


5  

My query looks like this "delete from Organization where some_key_id = ?" (am not deleting this based on primary key id)

我的查询看起来像这样“从组织中删除some_key_id =?” (我不是根据主键ID删除它)

A bulk delete (you should mention that in your question) doesn't cascade to anything. Quoting the JPA 1.0 specification:

批量删除(你应该在你的问题中提到)不会级联到任何东西。引用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.

删除操作仅适用于指定类及其子类的实体。它不会级联到相关实体。

This is a very annoying limitation and there are many RFEs to improve things (HHH-695, HHH-1917, HHH-3337, HHH-5529, etc).

这是一个非常烦人的限制,有很多RFE可以改善(HHH-695,HHH-1917,HHH-3337,HHH-5529等)。

For now, possible solutions include:

目前,可能的解决方案包括:

  1. clean up the child table yourself
  2. 自己清理孩子桌子
  3. use cascading foreign keys in the schema.
  4. 在架构中使用级联外键。

Now the weird part... My understanding of @OnDelete(action = OnDeleteAction.CASCADE) is that this annotation is supposed to be used to ensure that the foreign key is created with the appropriate ON DELETE CASCADE clause (solution #2). In other words, I'd expect things to work.

现在奇怪的部分......我对@OnDelete(action = OnDeleteAction.CASCADE)的理解是,这个注释应该用于确保使用适当的ON DELETE CASCADE子句创建外键(解决方案#2)。换句话说,我希望事情能发挥作用。

Did Hibernate generate the Organization table? Can you check the DDL? Do you see the expected ON DELETE CASCADE? If not, add it.

Hibernate是否生成了Organization表?你能看看DDL吗?你看到了预期的ON DELETE CASCADE吗?如果没有,请添加它。

#2


6  

Well, I guess you should add a

好吧,我想你应该添加一个

@Cascade(value = {DELETE, DELETE_ORPHAN})

Note that in JPA 2.0 you don't have to use the hibernate-sepcific @Cascade annotation - @*ToMany has an option to delete orphans.

请注意,在JPA 2.0中,您不必使用hibernate-sepcific @Cascade注释 - @ * ToMany可以选择删除孤儿。

Update: when using queries cascades are not handled. You have to handle them manually. This is expected and documented behaviour.

更新:使用查询时,不会处理级联。你必须手动处理它们。这是预期和记录的行为。