JpaRepository VS CrudRepository

In this topic, we will learn about the Jparepository vs crudrepository of the Spring Data JPA. JpaRepository and CrudRepository are the interfaces provided by the Spring Data JPA library. These interfaces are helping to reduce boilerplate codes for communicating with the database table and persistence operation on it. These repositories provide us with ready-made methods to execute SQL queries through them instead of writing manual queries in the data access layer. These repositories also allow us to create custom methods and native queries as per our needs.

jparepository_vs_crudrepository

JpaRepository

It is an interface given by Spring Data JPA. It is used to communicate with the JPA Entity and perform persistence operations on it with query method and native SQL query. This interface is imported from the package “org.springframework.data.repository”. It supports the full API methods of CrudRepository and PagingAndSortingRepository because it extends these interfaces. So it has basic CRUD, paging and sorting operations API. If we want to create a JPA Repository in our Spring Boot Application just create an interface and extend the JpaRepository interface. You can refer to this article on how to use JpaRepository in Spring Boot.

Example

public interface EmployeeRepository extends JpaRepository<Employee, Long> {}
  • Employee is a JPA entity class and Long is a data type of the property of that entity class which is annotated with @Id annotation.

CrudRepository

It is also an interface provided by Spring Data JPA. It is used to communicate with the JPA Entity and perform generic persistence CRUD operations. This interface is imported from the package “org.springframework.data.repository”. It extends the Repository interface of Spring Data JPA. It has less functionality as compared to JpaRepository.

Example

public interface EmployeeRepository extends CrudRepository<Employee, Long> {}
  • Employee is a JPA entity class and Long is a data type of the property of that entity class which is annotated with @Id annotation.

JpaRepository vs CrudRepository

Below here are tabular form differences between JpaRepository and CrudRepository. These are the following:

JpaRepositoryCrudRepository
It extends CrudRepository and PagingAndSortingRepository It extends Repository
It has supports the full API methods of CrudRepository and PagingAndSortingRepository. For example, flush(), saveAndFlush(), saveAllAndFlush(), deleteInBatch(), etc along with the methods that are present in CrudRepository.It has methods for CRUD operations(save(), saveAll(), findById(), findAll(), etc.).
This provides us with all the methods which are useful for implementing pagination.This doesn’t provide methods for implementing pagination and sorting features.
This works as a marker interface.This extends both CrudRepository and PagingAndSortingRepository.
This performs CRUD as well as batch operations and defines the repository extends JpaRepository.This performs CRUD operations and defines a repository extending the CrudRepository.
Syntax:
public interface JpaRepository<T,ID> extends PagingAndSortingRepository<T,ID>, QueryByExampleExecutor<T>
Syntax: 
public interface CrudRepository<T, ID> extends Repository<T, ID>

Conclusion

In this topic, we learnt the differences between the JpaRepository and the CrudRepository of the Spring Data JPA.

Leave a Comment