Spring Data JPA - JpaRepository methods deleteInBatch, getById, getOne are Deprecated

From recent version of Spring Data JPA JpaRepository methods like deleteInBatch(Iterable), getById(ID), and getOne(ID) are Deprecated. Instead use deleteAllInBatch(Iterable) and getReferenceById(ID).

The details are illustrated in below image:


The following example creates a Spring Boot web application which uses getReferenceById(ID) and deleteAllInBatch(Iterable) methods.

Project Directory



pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.knf.dev.demo</groupId>
<artifactId>spring-data-jpa-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-data-jpa-example</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>


Create Student Entity - Student.java

package com.knf.dev.demo.springdatajpaexample.entity;

import jakarta.persistence.*;

@Entity
@Table
public class Student {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(name="first_name")
private String firstName;

@Column(name="last_name")
private String lastName;

@Column(name="email_id")
private String emailId;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getEmailId() {
return emailId;
}

public void setEmailId(String emailId) {
this.emailId = emailId;
}

public Student(String firstName, String lastName, String emailId) {
this.firstName = firstName;
this.lastName = lastName;
this.emailId = emailId;
}

public Student() {
}

@Override
public String toString() {
return "Student{" +
"id=" + id +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", emailId='" + emailId + '\'' +
'}';
}
}


Create Student Repository - StudentRepository.java

package com.knf.dev.demo.springdatajpaexample.repository;

import com.knf.dev.demo.springdatajpaexample.entity.Student;
import org.springframework.data.jpa.repository.JpaRepository;

public interface StudentRepository extends JpaRepository<Student, Long> {
}


application.properties

spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=
true


Run the application - Application.java

package com.knf.dev.demo.springdatajpaexample;

import com.knf.dev.demo.springdatajpaexample.entity.Student;
import com.knf.dev.demo.springdatajpaexample.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@SpringBootApplication
@Transactional(readOnly = true)
public class Application implements CommandLineRunner {

@Autowired
StudentRepository studentRepository;

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

@Override
public void run(String... args) throws Exception {

Student student1 =
new Student("Alpha","Pro","alpha@gmail.in");
Student student2 =
new Student("Beta","Pro","beta@gmail.in");
Student student3 =
new Student("Gama","Pro","gama@gmail.in");

List<Student> students = Arrays.asList(student1,student2,student3);

//Save all students
studentRepository.saveAll(students);

//Find all students
List<Student> listOFStudents = studentRepository.findAll();
System.out.println(listOFStudents);

//getById(ID) is deprecated.
//Student std1 = studentRepository.getById(1l);
//System.out.println(std1);

//getOne(ID) is deprecated.
//Student std1s = studentRepository.getOne(1l);
//System.out.println(std1);

//Use getReferenceById(ID) instead.
Student std1 = studentRepository.getReferenceById(1l);
System.out.println(std1);

//deleteInBatch is deprecated.
//studentRepository.deleteInBatch(listOFStudents);

//Use deleteAllInBatch(Iterable) instead.
studentRepository.deleteAllInBatch(listOFStudents);

}
}

Application is the entry point that sets up the Spring Boot application. The @SpringBootApplication annotation enables auto-configuration and component scanning. 

Let's run this Spring boot application from either IntelliJ IDEA IDE by right click - Run 'Application.main()'
Or you can use the below maven command to run:

mvn spring-boot:run

Console Output:
Hibernate: 
    insert 
    into
        student
        (id, email_id, first_name, last_name) 
    values
        (default, ?, ?, ?)
Hibernate: 
    insert 
    into
        student
        (id, email_id, first_name, last_name) 
    values
        (default, ?, ?, ?)
Hibernate: 
    insert 
    into
        student
        (id, email_id, first_name, last_name) 
    values
        (default, ?, ?, ?)
Hibernate: 
    select
        s1_0.id,
        s1_0.email_id,
        s1_0.first_name,
        s1_0.last_name 
    from
        student s1_0
[Student{id=1, firstName='Alpha', lastName='Pro', emailId='alpha@gmail.in'}, Student{id=2, firstName='Beta', lastName='Pro', emailId='beta@gmail.in'}, Student{id=3, firstName='Gama', lastName='Pro', emailId='gama@gmail.in'}]
Student{id=1, firstName='Alpha', lastName='Pro', emailId='alpha@gmail.in'}
Hibernate: 
    delete 
    from
        student 
    where
        id=? 
        or id=? 
        or id=?

Java Spring Boot Full Stack Development Examples

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete