What is the difference between CrudRepository and JpaRepository interfaces in Spring Data JPA?
When I see the examples on the web, I see them there used kind of interchangeably.
What is the difference between them?
Why would you want to use one over the other?
Best Answer
JpaRepository
extendsPagingAndSortingRepository
which in turn extendsCrudRepository
.Their main functions are:
CrudRepository
mainly provides CRUD functions.PagingAndSortingRepository
provides methods to do pagination and sorting records.JpaRepository
provides some JPA-related methods such as flushing the persistence context and deleting records in a batch.Because of the inheritance mentioned above,
JpaRepository
will have all the functions ofCrudRepository
andPagingAndSortingRepository
. So if you don't need the repository to have the functions provided byJpaRepository
andPagingAndSortingRepository
, useCrudRepository
.