Erflow Stackov
Erflow Stackov

Reputation: 3

JPA Hibernate deleteAll(List<T> entities) not working as expected

I am trying to batch delete n objects with JPA repository deleteAll(List entities), and it works. But at the end it produces n queries to the database instead of grouping elements by the batch size.

Expected behaviour:

Actual behaviour:

If I do the batch update saveAll(List entities) I am getting expected behaviour with 2 queries produced.

Upvotes: 0

Views: 2244

Answers (3)

Gligorije Nikolic
Gligorije Nikolic

Reputation: 76

deleteAll(List entities) will delete all objects in one session, so the number of queries will be the n, grouped by the batch_size.

Upvotes: 1

S. Anushan
S. Anushan

Reputation: 788

By default deleteAll not support for batch operations it will iterator each object and hibernate will do the deletion you can check in the console logs(hibernate delete query).

For the batch deletion you can use below method. deleteAllInBatch();

Reference - https://docs.spring.io/spring-data/jpa/docs/current/api/org/springframework/data/jpa/repository/JpaRepository.html#deleteAllInBatch-java.lang.Iterable-

Upvotes: 0

Extend your repository from JpaRepository and try using deleteAllInBatch(List entities);

Upvotes: 0

Related Questions