Spring Data JPA CrudRepository deleteById() Example

In this article, we will see about Spring Data JPA CrudRepository deleteById() Example using Spring Boot and oracle.

Spring Data JPA CrudRepository deleteById() Example

The deleteById() method is used to delete an entity for a given id 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 deleteById() method of CrudRepository. The deleteById() method has been defined as below.

void deleteById(ID id);

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

Internally deleteById() method use EntityManger’s remove() method. When we use deleteById(), internally first findById() method get called and entityManager.remove() get called(as below dummy sample).

we will see about Spring Data JPA CrudRepository deleteById() Example.

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

 

Let’s see an example of Spring Data JPA CrudRepository deleteById() Example where we will use save() method for creating the entity, findById() to get a single record and deleteById() to delete a record.

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

 

StudentServiceImpl.java

SpringMain.java

we will see about Spring Data JPA CrudRepository deleteById() 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/save

Request Data –

Response data –

 

 

Get operation.

API -http://localhost:9091/student/{id}

http://localhost:9091/student/1

Spring Data JPA CrudRepository finById()

 

Delete operation.

Again perform get operation. We should get an error message. We can show an error message in proper way using global error handler. See Example here.

Spring Data JPA CrudRepository deleteById() Example

 

See brief about Spring Data JPA Repository hierarchy as below.

Spring Data JPA CrudRepository deleteById() Example

 

You may like.

Other Spring Data JPA and Hibernate tutorials.

Spring Data JPA findById() docs.

 

Top