Spring Data JPA deleteAll() Vs deleteAllInBatch()

In this tutorial, we will see about Spring Data JPA deleteAll() Vs deleteAllInBatch() Example using Spring Boot and Oracle.

The deleteAll() method is defined in CrudRepository has two overloaded versions.

First one – void deleteAll(Iterable<? extends T> entities)

Using the above method we can delete all entities which we pass as request data. The deleteAll() internally use delete() method only.

Spring Data JPA deleteAll() Vs deleteAllInBatch()

Second one – void deleteAll()

The above one will delete all records that belong to that repository. The deleteAll() internally uses findAll() and delete() method as below.

 

The query generated in case of deleteAll(). Suppose we are deleting twelve entities.

Hibernate: delete from student where id=?
Hibernate: delete from student where id=?
Hibernate: delete from student where id=?
Hibernate: delete from student where id=?
Hibernate: delete from student where id=?
Hibernate: delete from student where id=?
Hibernate: delete from student where id=?
Hibernate: delete from student where id=?
Hibernate: delete from student where id=?
Hibernate: delete from student where id=?
Hibernate: delete from student where id=?
Hibernate: delete from student where id=?

 

The deleteAllInBatch() method is defined in the JpaRepository interface as below.

void deleteAllInBatch()

Using deleteAllInBatch() method we can delete all entities from database. No need to pass entities as parameters.For example, let’s say we have an entity Student.java and also we have a corresponding repository class called StudentRepository.java something like below.

If we want to delete all student records we need to do studentRepository.deleteAllInBatch().  See the below sample code.

 

Note – Internally deleteAllInBatch()   uses createQuery() method as below.

 

In case of deleteAllInBatch() the query will generate as below.

Hibernate: delete from student

 

Both deleteAll() and deleteAllInBatch() use to delete all entities. The deleteAll() internally uses CrudRepository delete() method which further uses EntityManger remove() method which perform some other stuff. But in case of deleteAllInBatch(), EntityManger createQuery() method will preapare the query and it directly calls executeUpdate() method.

Spring Data JPA deleteAll() Vs deleteAllInBatch()

Let’s see an example of Spring Data JPA deleteAll() vs deleteAllInBatch() Method.

Open eclipse and create maven project, Don’t forget to check ‘Create a simple project (skip)’ click on next.  Fill all details(GroupId – springdatadeleteallvsdeleteallinbatch, ArtifactId – springdatadeleteallvsdeleteallinbatch and name – springdatadeleteallvsdeleteallinbatch) and click on finish. Keep packaging as the jar.

Modify pom.xml

Note – In pom.xml we have defined javac.exe path in configuration tag. You need to change accordingly i.e where you have installed JDK.

If you see any error for oracle dependency then follow these steps.

The directory structure for Spring Data JPA CrudRepository count() Example using Spring Boot and Oracle.

Directory structure –

Spring Data JPA CrudRepository count() Example

Spring Data JPA CrudRepository count() Example

Now see the code for Spring Data JPA deleteAll() Vs deleteAllInBatch().

Student.java

StudentController.java

StudentRepository.java – interface

StudentService.java – interface

Spring Data JPA CrudRepository count() Example

StudentServiceImpl.java

SpringMain.java

 

JpaConfig.java

application.properties

Let’s run the SpringMain class(run as java application).

Perform save operation first using below REST API.

Perform save operation first using below REST API.

http://localhost:9091/student/saveall

Request Data –

Response data –

 

 

 

DELETE  API – http://localhost:9091/student/deleteall

Spring Data JPA deleteAll() Vs deleteAllInBatch()

Let’s perform saveall again and delete all records using deleteAllInBatch().

Spring Data JPA deleteAll() Vs deleteAllInBatch()

 

 

 

That’s all about Spring Data JPA deleteAll() Vs deleteAllInBatch()  Example.

You may like.

Other Spring Data JPA and Hibernate tutorials.

Spring Data JPA JpaRepository() docs.

Spring Data JPA deleteAll() Vs deleteAllInBatch()

Top