Spring Data JPA CrudRepository delete() and deleteAll()

In this article, we will see about Spring Data JPA CrudRepository delete() and deleteAll() methods example using Spring Boot and oracle.

Spring Data JPA CrudRepository delete() and deleteAll()

The delete() method is used to delete a single entity which we pass as request data and it is available in CrudRepository interface. The CrudRepository extends Repository interface. In Spring Data JPA Repository is top-level interface in hierarchy. Here we are going to see delete() and deleteAll() method of CrudRepository. Let’s see about delete() method. The delete() method has been defined as below.

void delete(T entity);

Using delete() method we can delete a single record(entity). If we don’t provide any entity it will throw IllegalArgumentException.

Internally delete() method use EntityManger’s remove() method.

we wil

The deleteAll() method 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 CrudRepository delete() and deleteAll() Example

 

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.

 

Let’s see in below code how to use the Spring Data JPA CrudRepository delete() and deleteAll() method for delete operation.

 

Before go ahead see brief about Spring Data JPA Repository hierarchy as below.

Spring Data JPA CrudRepository delete() and deleteAll() Example

 

 

Let’s see an example of Spring Data JPA CrudRepository delete() and deleteAll() Example where we will use saveAll() method for creating the entities, findAll() to get all record, delete() to delete a single record and deleteAll() to delete all records.

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

 

 

Student.java

StudentController.java

StudentRepository.java – interface

Let’s see about Spring Data JPA CrudRepository deleteById().

StudentService.java – interface

Now we will define more classes for Spring Data JPA CrudRepository delete() and deleteAll() example.

 

StudentServiceImpl.java

SpringMain.java

N

Configurations files for Spring Data JPA CrudRepository delete() and deleteAll() example.

JpaConfig.java

application.properties

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

Perform save operation first using below REST API.

http://localhost:9091/student/saveall 

Request Data –

Response data –

 

 

 

Get operation. Let’s test we have records in DB or not.

API -http://localhost:9091/student/getall

Spring Data JPA CrudRepository delete() and deleteAll() Example

 

Now delete a single record.

http://localhost:9091/student/delete

Spring Data JPA CrudRepository delete() and deleteAll() Example

 

Delete all record which you are passing as request data.

Spring Data JPA CrudRepository delete() and deleteAll() Example

Now we don’t have any record in database. Check DB and Again perform get operation. We should have empty array. We can show a message in a proper way using global error handler. See Example here.

 

 

Still, we have one API to test.

http://localhost:9091/student/deleteall

First, call http://localhost:9091/student/saveall API then call http://localhost:9091/student/deleteall API. We should have below response and database shouldn’t contain any student records.

 

That’s all about Spring Data JPA CrudRepository delete() and deleteAll() Example.

 

You may like.

Other Spring Data JPA and Hibernate tutorials.

Spring Data JPA CrudRepository() docs.

This is all about Spring Data JPA CrudRepository delete() and deleteAll() Example.

Top