jpajpa-2.1hibernate-5.xhibernate-cascade

Hibernate 5 + JPA 2 doesn't cascade delete over join table


We have this relationship:

public class RuleProviderEntity implements Serializable
{
    ...
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @OrderColumn(name = RuleEntity.RULE_SEQUENCE)
    private List<RuleEntity> rules;
}

This alone creates a join table with 2 keys and the RULE_SEQUENCE column. So far good and works for SELECTs.

Now there's a JQL query

DELETE FROM RuleProviderEntity WHERE ...

But this fails to cascade deleting the RuleEntity rows. It just deletes the RuleProviderEntity and leaves the RuleEntity intact.

Is this supposed to work in JPA 2 and it's a Hibernate bug, or am I missing something in the config?

I know I could add @JoinTable but it would only override the defaults.
Also orphanRemoval seems not necessary here.
Maybe I could do a workaround with @PreRemove but not sure how.


Solution

  • You mean a JPQL Bulk Delete query is issued? rather than em.remove().

    A Bulk Delete query will NEVER respect cascade semantics and is not intended to (nor will it keep managed objects in-memory consistent with the datastore). If you want cascading then you need to call em.remove(). If in doubt about this look at the JPA spec.