Spring Data Repository does not delete ManyToOne Entity

Building on the excellent answer from user2936091 above, I just wanted to mention a (related) workaround I stumbled upon today: if the parent entity is not fetched into the Hibernate context, you are able to delete it directly.

In my case this was achieved by setting fetch = FetchType.LAZY on the @ManyToOne relationship. I wanted this change for performance reasons anyway, and noticed that without the parent eagerly fetched Hibernate was free to delete it via the repository method call.


The problem seems to be that you are using cascade=CascadeType.ALL, which also includes CascadeType.PERSIST. CascadeType.PERSIST means that the child entity is completely managed by the parent and you cannot delete it directly. In order to delete you just need to remove it from the parent.

You could just add the other CascadeTypes instead of all. e.g CascadeType.REMOVE, if the only thing you would want is to remove the child if the parent is removed.