Spring Data JPA deleteInBatch() Example

In this post, we will see Spring Data JPA deleteInBatch() example using Spring Boot and Oracle.

Let’s see some points about deleteInBatch() method.

The deleteInBatch() has been defined in the JpaRepository interface with the following signature.

void deleteInBatch(Iterable<T> entities);

We can delete one or multiple entities using deleteInBatch() method.

The deleteInBatch() internally implemented as below.

Spring Data JPA deleteInBatch() Example.

The deleteInBatch() method prepares the query and collect some other information and it calls the executeUpdate() method.

The deleteInBatch() method throw IllegalArgumentException if an entity is null and internally annotated with @Transactional annotation.

 

Let’s see how to use deleteInBatch(). We will see a complete example later.

First, define repository interface extending JpaRepository since deleteInBatch() defined in JpaRepository interface.

In ServiceImpl class we can use deleteInBatch() method as below.

 

The query generated if we delete multiple entities(12 entities).

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

Let’s see an example of Spring Data JPA deleteInBatch() Using Spring Boot.

Open eclipse and create maven project, Don’t forget to check ‘Create a simple project (skip)’ click on next.  Fill all details(GroupId – springdatadeleteinbatch, ArtifactId – springdatadeleteinbatch and name – springdatadeleteinbatch) 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.

Directory structure –

Spring Data JPA delete() vs deleteInBatch()

 

 

Student.java

StudentController.java

StudentRepository.java – interface

StudentService.java – interface

Note – See here more about @Component, @Controller, @Service and @Repository annotations here.

StudentServiceImpl.java

Note – See here more about @Component, @Controller, @Service and @Repository annotations here.

 

SpringMain.java

 

JpaConfig.java

Note – See more details about @Configuration annotations here.

application.properties

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

Perform saveall operation first using below REST API.

http://localhost:9091/student/saveall

Request Data.

 

Response data –

 

 

Perform deleteinbatch operation using below REST API.

http://localhost:9091/student/deleteinbatch

Request Data – Use response data of  http://localhost:9091/student/saveall API.

 

 

Spring Data JPA delete() vs deleteInBatch()

Since we have deleted all record, let’s perform save all operation once again. This time we delete one record using delete() and deleteInBaatch() method.

Perform save all operation using –  http://localhost:9091/student/saveall

Spring Data JPA delete() vs deleteInBatch()

 

 

Delete single record using deleteInBatch() method.

http://localhost:9091/student/deleteinbatch

Spring Data JPA delete() vs deleteInBatch()

 

That’s all about Spring Data JPA deleteInBatch() Example using Spring Boot and Oracle.

You may like.

Other Spring Data JPA and Hibernate tutorials.

Spring Data JPA deleteInBatch() docs.

 

Summary – The deleteInBatch() method has been defined in the JpaRepository interface used to delete multiple entities and it will create a single query.

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

 

 

 

Top