CrudRepository findAllById() Example Using Spring Boot

In this tutorial, we will see CrudRepository findAllById() Example Using Spring Boot. The findAllById() method is used to retrieve multiple entities of the type with the given IDs. The CrudRepository extends Repository interface. In Spring Data JPA Repository is top-level interface in hierarchy. Here we are going to see findAllById() method of CrudRepository. The findAllById() method has been defined as below.

Iterable<T> findAllById(Iterable<ID> ids);

CrudRepository findAllById() Example Using Spring Boot

 

Using findAllById() method we can get multiple records(entities) on the basis of given ids.

Internally findAllById() method use findById() method only as below.

 

Note – By default, the findAllById() method returns the entities in sorted order(ascending order) on the basis of ids.

CrudRepository findAllById() Example Using Spring Boot

 

Request data to save multiple entities.

 

 

Providing multiple ids from the postman.

Response –

 

 

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

 

Let’s see an example of Spring Data JPA CrudRepository findAllById() Example where we will use saveAll() method for creating the entity and findAllById() to get all records on basis of ids.

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

CrudRepository findAllById() Example Using Spring Boot

If you see any error for oracle dependency then follow these steps.

Directory structure –

Spring Data JPA CrudRepository finById()

 

 

Student.java

IdContainer.java

CrudRepository findAllById() Example Using Spring Boot

 

StudentController.java

StudentRepository.java – interface

StudentService.java – interface

 

StudentServiceImpl.java

SpringMain.java

 

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

 

Get operation basis of ids.

http://localhost:9091/student/findallbyid

 

 

Generated Query.

Hibernate: select student0_.id as id1_0_, student0_.name as name2_0_, student0_.roll_number as roll_number3_0_, student0_.university as university4_0_ from student student0_ where student0_.id in (? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ?)

 

 

CrudRepository findAllById() Example Using Spring Boot

 

 

That’s all about CrudRepository findAllById() Example Using Spring Boot.

 

You may like.

 

You may like.

Other Spring Data JPA and Hibernate tutorials.

Spring Data JPA findAllById() docs.

 

 

We have seen CrudRepository findAllById() Example Using Spring Boot.

Top