Bulk DELETE no longer cascades

In previous releases a JPQL or Criteria bulk DELETE against an entity with a single-valued cascade-delete relation - a @OneToOne or @ManyToOne with cascade=REMOVE or orphanRemoval=true, or OpenJPA’s own @Dependent - was executed in memory: every instance was loaded and removed through the persistence context, cascading to related entities and, as a side effect, cleaning up the rows of the join tables and element collection tables of the candidate. As required by section 4.10 of the specification, bulk operations do not cascade. Related entities are left untouched by a DELETE query and will cause foreign key violations unless the database defines ON DELETE CASCADE.

A candidate without such a relation - one that only has an @ElementCollection, or a collection whose cascade is declared on the elements, as @OneToMany(cascade=REMOVE), orphanRemoval=true and @ElementDependent do - already took the bulk SQL path, and the rows of its join tables and element collection tables were left behind on the deleted primary keys. Those rows are now removed, so for such a candidate the behaviour described below is a fix rather than a replacement for cascading that was lost.

The rows of the join tables and element collection tables owned by the deleted entities are not entities themselves and are removed. A DELETE query against a candidate that owns such a table therefore executes as a sequence of statements rather than as one: the criteria are evaluated once by a SELECT DISTINCT <pk> FROM <entity table> WHERE …​, then a DELETE FROM <owned table> WHERE <join column> IN (…​) is issued for every owned table and finally a DELETE FROM <entity table> WHERE <pk> IN (…​) for the entity rows. The keys are materialized before anything is deleted, so no statement re-evaluates the criteria against an already modified database, and the IN lists are split into chunks the database accepts. The reported update count is the number of deleted entity rows. A candidate with a composite primary key, or with a join foreign key that does not consist of a single column referencing that primary key, is executed in memory instead.

A DELETE query that carries no criteria at all - no WHERE clause and no discriminator or subclass condition - matches every candidate row, so every row of every owned table belongs to a deleted candidate. Such a query skips the key select and issues an unqualified DELETE FROM <owned table> for every owned table, followed by the unqualified delete of the entity table. Any condition whatsoever falls back to the key based path described above.

Three consequences are worth noting for a filtered delete. The primary keys of all matching candidates are held in memory while the query runs, so a delete that matches millions of rows now needs memory proportional to the number of matches. A configured query timeout is applied to each statement, which turns it into a per-chunk budget rather than a budget for the whole operation. And the criteria are evaluated by a statement of their own, separate from the deletes, so under READ COMMITTED a row that stops matching between the key select and the delete is still deleted.

One case is not covered: for a uni-directional self-referencing @ManyToMany the join table is only cleaned by the owner column, so rows whose inverse column references a deleted primary key remain. This is a pre-existing limitation; delete those rows explicitly or define ON DELETE CASCADE on both foreign keys.

Set openjpa.Compatibility=CleanupOwnedTablesOnBulkDelete=false (default true) to suppress the cleanup. This does not restore the behavior of any released version - the cleanup ships with 4.2.0 - it only leaves the rows of the owned tables behind. Note that a candidate which owns a bi-directional join table can only be bulk deleted in SQL when the cleanup is enabled; with the option turned off such a query is executed in memory, which loads the matching instances and deletes them one row at a time - a SELECT of the candidates followed by one DELETE FROM <entity table> WHERE <pk> = ? per instance instead of a single bulk statement - and, because the rows of a bi-directional join table are maintained by the inverse side, still leaves those join table rows behind.

Delete dependent entities explicitly (separate bulk deletes or em.remove()) or rely on database-level cascades. A bulk UPDATE or DELETE that still has to be executed in memory now flushes the persistence context, which is described in An in-memory bulk operation flushes the persistence context. See also JPQL Bulk Update and Delete.