Spring Boot is built on the top of the spring and contains all the features of spring. And is becoming a favorite of developers these days because of its rapid production-ready environment which enables the developers to directly focus on the logic instead of struggling with the configuration and setup. Spring Boot is a microservice-based framework and making a production-ready application in it takes very little time.
CRUD Repository
There is an interface available in Spring Boot named as CrudRepository that contains methods for CRUD operations. It provides generic Crud operation on a repository. It is defined in the package org.springframework.data.repository and It extends the Spring Data Repository interface. If someone wants to use CrudRepository in the spring boot application he/she has to create an interface and extend the CrudRepository interface.
Syntax:
public interface CrudRepository<T, ID> extends Repository<T, ID>
Where:
- T: Domain type that repository manages (Generally the Entity/Model class name)
- ID: Type of the id of the entity that repository manages (Generally the wrapper class of your @Id that is created inside the Entity/Model class)
Example:
public interface DepartmentRepository extends CrudRepository<Department, Long> {}
JpaRepository
JpaRepository is a JPA (Java Persistence API) specific extension of Repository. It contains the full API of CrudRepository and PagingAndSortingRepository. So it contains API for basic CRUD operations and also API for pagination and sorting.
Syntax:
public interface JpaRepository<T,ID> extends PagingAndSortingRepository<T,ID>, QueryByExampleExecutor<T>
Where:
- T: Domain type that repository manages (Generally the Entity/Model class name)
- ID: Type of the id of the entity that repository manages (Generally the wrapper class of your @Id that is created inside the Entity/Model class)
Example:
public interface DepartmentRepository extends JpaRepository<Department, Long> {}
Spring Data Repository Interface
Here in the following image Repository, CrudRepository and PagingAndSortingRepository belong to Spring Data Commons whereas JpaRepository belongs to Spring Data JPA.
Difference Table
CrudRepository |
JpaRepository |
---|---|
It is a base interface and extends Repository Interface. | It extends PagingAndSortingRepository that extends CrudRepository. |
It contains methods for CRUD operations. For example save(), saveAll(), findById(), findAll(), etc. | It contains the full API of CrudRepository and PagingAndSortingRepository. For example, it contains flush(), saveAndFlush(), saveAllAndFlush(), deleteInBatch(), etc along with the methods that are available in CrudRepository. |
It doesn’t provide methods for implementing pagination and sorting | It provides all the methods for which are useful for implementing pagination. |
It works as a marker interface. | It extends both CrudRepository and PagingAndSortingRepository. |
To perform CRUD operations, define repository extending CrudRepository. | To perform CRUD as well as batch operations, define repository extends JpaRepository. |
Syntax: public interface CrudRepository<T, ID> extends Repository<T, ID> |
Syntax: public interface JpaRepository<T,ID> extends PagingAndSortingRepository<T,ID>, QueryByExampleExecutor<T> |