What Is Difference Between Crudrepository and Jparepository Interfaces in Spring Data Jpa?
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...
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?
7 Answers
JpaRepository extends PagingAndSortingRepository which in turn extends CrudRepository.
Their main functions are:
CrudRepositorymainly provides CRUD functions.PagingAndSortingRepositoryprovides methods to do pagination and sorting records.JpaRepositoryprovides 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 of CrudRepository and PagingAndSortingRepository. So if you don't need the repository to have the functions provided by JpaRepository and PagingAndSortingRepository , use CrudRepository.
Ken's answer is basically right but I'd like to chime in on the "why would you want to use one over the other?" part of your question.
Must Read
Basics
The base interface you choose for your repository has two main purposes. First, you allow the Spring Data repository infrastructure to find your interface and trigger the proxy creation so that you inject instances of the interface into clients. The second purpose is to pull in as much functionality as needed into the interface without having to declare extra methods.