- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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. | Key | JPARepository | CrudRepository |
---|---|---|---|
1 | Hierarchy | JPA extend crudRepository and PagingAndSorting repository | Crud Repository is the base interface and it acts as a marker interface. |
2 | Batch support | JPA 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. |
3 | Pagination support | JPA 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. |
4 | Use Case | JpaRepository 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
@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); }