Difference Between CrudRepository and JPARepository in Java

CrudRepository and JPA repository both are the interface of the spring data repository library. Spring data repository reduces the boilerplate code by providing some predefined finders to access the data layer for various persistence layers.

JPA repository extends CrudRepository and PagingAndSorting repository. It inherits some finders from crud repository such as findOne, gets and removes an entity. It also provides some extra methods related to JPA such as delete records in batch, flushing data directly to a database base and methods related to pagination and sorting.

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

Sr. No.KeyJPARepositoryCrudRepository
1HierarchyJPA extend crudRepository and PagingAndSorting repositoryCrud Repository is the base interface and it acts as a marker interface.
2Batch supportJPA also provides some extra methods related to JPA such as delete records in batch and flushing data directly to a database.It provides only CRUD functions like findOne, saves, etc.
3Pagination supportJPA repository also extends the PagingAndSorting repository. It provides all the method for which are useful for implementing pagination.Crud Repository doesn't provide methods for implementing pagination and sorting.
4Use CaseJpaRepository ties your repositories to the JPA persistence technology so it should be avoided.We should use CrudRepository or PagingAndSortingRepository depending on whether you need sorting and paging or not.

Example of JpaRepository

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

Example of CrudRepository

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

Updated on: 18-Nov-2019

15K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started