JpaRepository's deleteAllInBatchByField not deleting directly

14 Views Asked by At

JpaRepository has two methods deleteAll() and deleteAllInBatch(); the latter uses a DELETE statement, while the former fetches all entities into persistence context and deletes them individually, which ensures all validations and events are fired.

I want to use the in-batch variant with a where clause, however it still fetches the entities.

public interface FooRepository extends JpaRepository<Foo, Long> {

    void deleteAllByField(String field);

    void deleteAllInBatchByField(String field);

    @Modifying
    @Query("delete from Foo where field=:field")
    void customDelete(String field);
}

The deleteAllByField and deelteAllInBatchByField in my case have the same effect: they execute a single SELECT and many DELETE statements. Also removing the All keyword in name has no effect. The customDelete works as expected, it executes a single DELETE statement.

Why could that be? If the InBatch or All keywords have no effect, why don't they produce an error, such as non-existent field after By would? Spring Data documentation doesn't mention delete methods at all.

0

There are 0 best solutions below