This article is participating in the Java Theme Month – Java Debug Notes Event, see the event link for details
The problem
What is the difference between CrudRepository and JpaRepository in Spring Data JPA?
When I looked for examples on the Internet, I realized that they can be used interchangeably.
How are they different? Why do you prefer one over the other?
answer
Answer 1
JpaRepository expanded the PagingAndSortingRepository, which expanded the CrudRepository PagingAndSortingRepository.
Their main functions:
CrudRepository
Mainly provide CRUD (add, delete, change and check) function;PagingAndSortingRepository
Provides methods to perform paging and sorting of data;JpaRepository
Provides jPA-related methods, such as refreshing the persistence context and deleting records in the batch;
And according to the above said inheritance, JpaRepository will have PagingAndSortingRepository and CrudRepository all the functions provided. So, if you don’t need to use PagingAndSortingRepository and JpaRepository provides additional capabilities, then using CrudRepository is enough.
Answer 2
Answer 1 is absolutely correct, but I would like to explain why you are accustomed to using one over the other.
-
Basic knowledge of
In general, the base ports we chose for our repository serve two main purposes. One is to allow the Spring JPA repository infrastructure to look up corresponding interfaces and trigger proxy creation to inject interface instances into clients. The other is to introduce as much functionality as possible into the interface without declaring additional methods.
-
Common interface
The Spring Data core library provides two basic interfaces:
CrudRepository
-CRUD (add, delete, modify and check) method;PagingAndSortingRepository
– extensionCrudRepository
, mainly paging and sorting methods;
-
Specific storage interface
Individual storage modules, such as JPA or MongoDB, expose extended interfaces for specific storage to facilitate access to specific storage functionality, such as refreshing or dedicated batch operations.
These specific interfaces are generally not recommended because they expose the underlying persistent interfaces to clients, reinforcing the coupling between them and the repository.
-
User-defined storage ports
There are two major disadvantages to relying on the provided generic interface:
- Depending on the Spring Data interface, the repository interface is coupled to the repository;
- By extending the
CrudRepository
, will immediately expose the persistence method;
-
conclusion
Repository abstraction allows us the freedom to choose base libraries that are driven entirely by architectural and functional requirements. If appropriate, we can use the out-of-the-box method. If necessary, we can design our own storage interface. Do not use specific storage ports unless necessary.
Answer 3
Conclusion:
JpaRepository
Extend thePagingAndSortingRepository
;PagingAndSortingRepository
Extend theCrudRepository
;
CrudRepository provides methods for CRUD operations on the interface, so it allows you to create, read, update, and delete records without having to define methods yourself.
PagingAndSortingRepository provides other methods using paging and sorting retrieval entity;
provenance
Stack Overflow: What is Difference Between CrudRepository and JpaRepository interfaces in Spring Data JPA?