I have 2 entities like this:
@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
}
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.
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 ?
The delete operation :
Parent parent= entityManager.find(Parent.class, id);
entityManager.remove(parent);
I'm not sure if what you want is possible, but you could try adding this to your mapping:
@OnDelete(action = OnDeleteAction.NO_ACTION)
Maybe with this Hibernate won't change the relationship.
作为解决方法,您可以关闭级联删除并手动删除子项
You need to:
@OneToMany(cascade = CascadeType.ALL)
with @OneToMany(cascade = CascadeType.PERSIST)
.nullable = false
to @JoinColumn(name = "parent_id")
annotation.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.