English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

The Difference Between CrudRepository and JPARepository in Java

Text

CrudRepository and JpaRepository are interfaces of the Spring Data repository library. Spring Data repositories access various data layers in the persistence layer by providing some predefined finders, thus reducing boilerplate code.

We need to extend this repository in the application before we can access all the methods available in these repositories. We can also add new methods using named queries or native queries based on business requirements.

Serial numberKeyJPAR repositoryCrud repository
1HierarchyJPA extends the crudRepository and PagingAndSorting repositoryThe original repository is a basic interface that acts as a marker interface.
2Batch supportJPA also provides some methods related to JPA, such as batch deletion of records and directly refreshing data to the database.It only provides CRUD functions, such as findOne, save, etc.
3Pagination supportJPA repositories also extend the PagingAndSorting repository. It provides all methods that can be used for pagination.Crud repositories do not provide methods for implementing pagination and sorting.
4Use casesJpaRepository connects your repository with JPA persistence technology, so it should be avoided.We should use CrudRepository or PagingAndSortingRepository depending on whether you need sorting and pagination.

Example of JpaRepository

@Repository
public interface BookDAO extends JpaRepository {
   Book findByAuthor(@Param("id") Integer id);
{}

Example of CrudRepository

@Repository
public interface BookDAO extends CrudRepository {
   Book Event findById(@Param("id") Integer id);
{}