休眠软删除将外键设置为null [英] Hibernate soft delete sets foreign key to null

查看:98
本文介绍了休眠软删除将外键设置为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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆