CrudRepository Examples in Spring Data JPA

CrudRepository Examples in Spring Data JPA | We will see the commonly used methods of the CurdRepository along with the examples. Create a Spring Boot project and include the following dependencies:- Spring Data JPA, Lombok, MySQL Driver.

application.yml

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/test
    username: root
    password: root
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true

Entity class:-

package com.knowprogram.demo.model;

import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Entity
@Table
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student {
    @Id
    private Integer id;

    private String name;

    private Double fee;
}

Repository:-

package com.knowprogram.demo.repository;

import org.springframework.data.repository.CrudRepository;

import com.knowprogram.demo.model.Student;

public interface StudentRepository extends CrudRepository<Student, Integer> {

}

Runner for testing:-

package com.knowprogram.demo.runner;

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class TestRunner implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {
        System.out.println("TestRunner.run()");
        // code
    }

}

After running the application, it will create the table and execute the run() method:-

Hibernate: create table student (id integer not null auto_increment, fee float(53), name varchar(255), primary key (id)) engine=InnoDB
TestRunner.run()

save() Method

The save(s) method behaves like either insert or update. It supports taking Model class objects and even their sub-class objects. Method signature: <S extends T> S save(S entity);

This method first executes a SELECT query with a given ID in the object. Checks Given ID exist or not? If the ID does not exist in the DB table then INSERT, else Update.

package com.knowprogram.demo.runner;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import com.knowprogram.demo.model.Student;
import com.knowprogram.demo.repository.StudentRepository;

@Component
public class TestRunner implements CommandLineRunner {

    @Autowired
    private StudentRepository studentRepository;

    @Override
    public void run(String... args) throws Exception {
        Student s1 = new Student(10, "A", 2.2);
        studentRepository.save(s1);

        Student s2 = new Student(10, "B", 2.2);
        studentRepository.save(s2);
    }

}
Hibernate: select s1_0.id,s1_0.fee,s1_0.name from student s1_0 where s1_0.id=?
Hibernate: insert into student (fee,name,id) values (?,?,?)
Hibernate: select s1_0.id,s1_0.fee,s1_0.name from student s1_0 where s1_0.id=?
Hibernate: update student set fee=?,name=? where id=?

For the save(s1) method call it first executes the select query. Since the record is not found therefore it inserts the record. On the save(s2) method call again it executes the select query and finds the record therefore it updates the existing record. Insert a few more records:-

studentRepository.save(new Student(101, "A", 2.2));
studentRepository.save(new Student(102, "B", 3.2));
studentRepository.save(new Student(103, "C", 4.2));
studentRepository.save(new Student(104, "D", 5.2));

Instead of calling the save() method for each time and making multiple database calls, we can use the saveAll() method for bulk insert.

saveAll() Method

This method is used to insert/update multiple rows at a time. Iterable is the supertype for collections. So, we can choose any one Collection. Method signature:- <S extends T> Iterable<S> saveAll(Iterable<S> entities);

package com.knowprogram.demo.runner;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import com.knowprogram.demo.model.Student;
import com.knowprogram.demo.repository.StudentRepository;

@Component
public class TestRunner implements CommandLineRunner {

    @Autowired
    private StudentRepository studentRepository;

    @Override
    public void run(String... args) throws Exception {
        studentRepository.saveAll(
                List.of(
                   new Student(105, "A", 5.1),
                   new Student(106, "B", 6.1),
                   new Student(107, "C", 7.1),
                   new Student(108, "D", 8.1)
                )
            );
    }

}
Hibernate: select s1_0.id,s1_0.fee,s1_0.name from student s1_0 where s1_0.id=?
Hibernate: select s1_0.id,s1_0.fee,s1_0.name from student s1_0 where s1_0.id=?
Hibernate: select s1_0.id,s1_0.fee,s1_0.name from student s1_0 where s1_0.id=?
Hibernate: select s1_0.id,s1_0.fee,s1_0.name from student s1_0 where s1_0.id=?
Hibernate: insert into student (fee,name,id) values (?,?,?)
Hibernate: insert into student (fee,name,id) values (?,?,?)
Hibernate: insert into student (fee,name,id) values (?,?,?)
Hibernate: insert into student (fee,name,id) values (?,?,?)

The List.of() method was introduced in the JDK 9 version, for older versions you can use Arrays.asList() method.

Java 8 introduced the Optional<T> class to avoid NullPointerException and handle null values. Dynamic Data(UI/DB/File) may be the null value. So, before processing it, a null check must be done.

findById() Method

This method is used to fetch data from DB using the primary key value. Given ID may or may not exist in DB therefore, the return type is Optional<T>. Method signature:- Optional<T> findById(ID id);

package com.knowprogram.demo.runner;

import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import com.knowprogram.demo.model.Student;
import com.knowprogram.demo.repository.StudentRepository;

@Component
public class TestRunner implements CommandLineRunner {

    @Autowired
    private StudentRepository studentRepository;

    @Override
    public void run(String... args) throws Exception {
        Optional<Student> optional = studentRepository.findById(105);
        if (optional.isPresent()) {
            Student student = optional.get();
            System.out.println(student);
        } else {
            System.out.println("Data not found");
        }
    }

}
Hibernate: select s1_0.id,s1_0.fee,s1_0.name from student s1_0 where s1_0.id=?
Student(id=105, name=A, fee=5.1)

If we try to fetch an ID that does not exist:-

Optional<Student> optional = studentRepository.findById(109);
Hibernate: select s1_0.id,s1_0.fee,s1_0.name from student s1_0 where s1_0.id=?
Data not found

We can also throw exceptions:-

Student student = studentRepository.findById(109)
                  .orElseThrow(() -> new Exception("ID Not Found"));
System.out.println(student);

existsById() Method

If a given ID exists in the database table then it returns true else it returns false. Method signature:- boolean existsById(ID id);

boolean exist = studentRepository.existsById(10);
System.out.println(exist);
Hibernate: select count(*) from student s1_0 where s1_0.id=?
true

What are cursors in Java and which one is added in JDK 1.8? Enumerator, Iterator, ListIterator, and SplitIterator. The SpliIterator was introduced in the JDK 1.8 version.

findAll() Method

This method is used to fetch all rows from the database table. SQL: SELECT * FROM <TABLE-NAME>; Method signature:- Iterable<T> findAll();

It will fetch data from the database table using the select query into List<T> Collection, casted to Iterable, that supports reading data in.

Iterable<Student> iterable = studentRepository.findAll();
iterable.forEach(System.out::println);
Hibernate: select s1_0.id,s1_0.fee,s1_0.name from student s1_0
Student(id=10, name=B, fee=2.2)
Student(id=105, name=A, fee=5.1)
Student(id=106, name=B, fee=6.1)
Student(id=107, name=C, fee=7.1)
Student(id=108, name=D, fee=8.1)

Different ways to display the data:-

Iterable<Student> itb = studentRepository.findAll();
System.out.println(itb.getClass().getName()); // ArrayList

Iterable<Student> itb = studentRepository.findAll();
System.out.println(itb.getClass().getName()); // ArrayList

// Different ways to display the record
// Java 8 - Method Reference
itb.forEach(System.out::println);
System.out.println("--------------------");

// Java 8 - Lambda Expression
itb.forEach(s -> System.out.println(s));
System.out.println("--------------------");

// Iterator
Iterator<Student> itr = itb.iterator();
while (itr.hasNext()) {
   Student s = itr.next();
   System.out.println(s);
}
System.out.println("--------------------");

// For Each Loop
for (Student s : itb) {
   System.out.println(s);
}

findAllById() Method

To fetch the random selected rows using the IN operator we can use this method. SQL: select * from student where sid in (_, _, _);

Iterable<Student> itb = studentRepository.findAllById(List.of(105, 106));
itb.forEach(System.out::println);
Hibernate: select s1_0.id,s1_0.fee,s1_0.name from student s1_0 where s1_0.id in (?,?)

count() Method

This method is used to calculate the total number of rows in the database table. SQL: select count(*) from student; Method signature:- long count();

long count = studentRepository.count();
System.out.println(count);
Hibernate: select count(*) from student s1_0
5

deleteById() Method

This method is used to delete one row based on the primary key(ID). Method Signature:- void delete(T entity);

This method is checking given id exist or not in DB table using internally findById() method, if exist then it is calling delete(obj) method, else throws EmptyResultDataAccessException.

studentRepository.deleteById(105);
Hibernate: select s1_0.id,s1_0.fee,s1_0.name from student s1_0 where s1_0.id=?
Hibernate: delete from student where id=?

delete() Method

This method takes an object having the primary key. If the given ID exists in DB it will delete
else does nothing. Note that, if id does not exist then it won’t give any exception, no error. Method Signature:- void delete(T entity);

deleteAll(Iterable) Method

It takes an object with the primary key as a List of objects. If the given object id is matching with the table row then it will delete such row else does nothing no exception, no error. Method Signature:- void deleteAll(Iterable<? extends T> entities);

Iterable<Student> selectedStudents = studentRepository.findAllById(List.of(106, 107));
System.out.println(selectedStudents);
studentRepository.deleteAll(selectedStudents);
Hibernate: select s1_0.id,s1_0.fee,s1_0.name from student s1_0 where s1_0.id in (?,?)
[Student(id=106, name=B, fee=6.1), Student(id=107, name=C, fee=7.1)]
Hibernate: select s1_0.id,s1_0.fee,s1_0.name from student s1_0 where s1_0.id=?
Hibernate: select s1_0.id,s1_0.fee,s1_0.name from student s1_0 where s1_0.id=?
Hibernate: delete from student where id=?
Hibernate: delete from student where id=?

Another example:- In the database, id 108 exists therefore the deleteAll will delete that record but id 109 does not exist therefore it won’t do anything for id 109.

studentRepository.deleteAll(List.of(new Student(108, "", 1.0), new Student(109, "", 1.0)));
Hibernate: select s1_0.id,s1_0.fee,s1_0.name from student s1_0 where s1_0.id=?
Hibernate: select s1_0.id,s1_0.fee,s1_0.name from student s1_0 where s1_0.id=?
Hibernate: delete from student where id=?

deleteAll() Method

It will delete all rows in the database table. Method signature:- void deleteAll();

studentRepository.deleteAll();
Hibernate: select s1_0.id,s1_0.fee,s1_0.name from student s1_0
Hibernate: delete from student where id=?
Hibernate: delete from student where id=?
Hibernate: delete from student where id=?
Hibernate: delete from student where id=?

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *