Hibernate soft delete sets foreign key to null

I have a parent entity 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);

1 answer

  • answered 2017-10-20 13:12 BrenoQ

    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.