休眠软删除将外键设置为null [英] Hibernate soft delete sets foreign key to null
问题描述
我有2个这样的实体:
@SQLDelete(sql = "UPDATE parent_table SET deleted = true WHERE id = ?")
public class Parent {
private boolean deleted;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "parent_id")
private List<Child> children;
// other stuff
}
@SQLDelete(sql = "UPDATE child_table SET deleted = true WHERE id = ?")
public class Child {
private boolean deleted;
// stuff
}
如您所见,
它是一个单向@OneToMany映射,并且两个实体都使用带有@SQLDelete
批注的软删除.
我正在尝试软删除父级,然后又希望子级也被软删除.
As you can see, its a unidirectional @OneToMany mapping and both entities use soft delete with the @SQLDelete
annotation.
I'm trying to soft delete the parent and in turn want the child to be soft deleted as well.
当我尝试软删除时,它将两个表中的deleted
标志设置为true,这就是我想要的.
但是,当我执行删除操作时,在child_table
中的parent_id
被设置为null
.为什么会发生这种情况,我该如何阻止呢?
When I try to soft delete , it sets the deleted
flag to true in both tables and that's what I want.
However, the parent_id
in the child_table
is set to null
when I perform the delete. Why is this happening and how can I stop this ?
删除操作:
Parent parent= entityManager.find(Parent.class, id);
entityManager.remove(parent);
推荐答案
作为一种解决方法,您可以关闭级联删除并手动删除子级
As a workaround you can turn off cascade delete and manually delete the children
这篇关于休眠软删除将外键设置为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!