Spring Data JPA JpaRepository deleteAllInBatch()

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

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

 

Note – We will see a complete example for JpaRepository deleteAllInBatch() method but before going ahead let’s see how it is different from CrudRepository deleteAll() method. Both deleteAll() and deleteAllInBatch() use to delete all entities. The deleteAll() internally uses CrudRepository delete() method which further uses EntityManger remove() method which perform a lot of stuff. But in case of deleteAllInBatch(), EntityManger createQuery() method will preapare the query and it will directly call executeUpdate() method.

 

Let’s see an example of Spring Data JPA JpaRepository 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 – springdatadeleteallinbatch, ArtifactId – springdatadeleteallinbatch and name – springdatadeleteallinbatch) 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 JpaRepository deleteAllInBatch() Example using Spring Boot and Oracle.

Directory structure –

Spring Data JPA JpaRepository deleteAllInBatch()

Spring Data JPA CrudRepository count() Example

 

Student.java

StudentController.java

StudentRepository.java – interface

StudentService.java – interface

Spring Data JPA CrudRepository count() Example

StudentServiceImpl.java

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

SpringMain.java

Note – See more details about @ComponentScan here.

JpaConfig.java

Note – See more details about @Configuration annotations here.

 

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 –

 

 

 

Let’s delete all records using deleteAllInBatch().

http://localhost:9091/student/deleteallinbatch

 

Spring Data JPA JpaRepository deleteAllInBatch()

 

 

 

That’s all about Spring Data JPA CrudRepository count() Example.

You may like.

Other Spring Data JPA and Hibernate tutorials.

Spring Data JPA CrudRepository() docs.

Spring Data JPA JpaRepository deleteAllInBatch() Example.

Top