Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 of interchangeably.

What is the difference between them?

Why would you want to use one over the other?

like image 278
kseeker Avatar asked Dec 23 '12 19:12

kseeker


People also ask

Which is better CrudRepository or JpaRepository?

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 .

What is true about CrudRepository and JpaRepository in Spring data JPA?

CrudRepository provides CRUD functions. PagingAndSortingRepository provides methods to do pagination and sort records. JpaRepository provides JPA related methods such as flushing the persistence context and delete records in a batch.

What is the difference between a CrudRepository and a JpaRepository What is the difference between a CrudRepository and a JpaRepository?

CrudRepository extends Repository interface whereas JpaRepository extends PagingAndSortingRepository and QueryByExampleExecutor interface. PagingAndSortingRepository further extends CrudRepository only.

What is JpaRepository interface?

JpaRepository is a JPA (Java Persistence API) specific extension of Repository. It contains the full API of CrudRepository and PagingAndSortingRepository. So it contains API for basic CRUD operations and also API for pagination and sorting.


1 Answers

JpaRepository extends PagingAndSortingRepository which in turn extends CrudRepository.

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 of CrudRepository and PagingAndSortingRepository. So if you don't need the repository to have the functions provided by JpaRepository and PagingAndSortingRepository , use CrudRepository.

like image 125
Ken Chan Avatar answered Sep 28 '22 05:09

Ken Chan