What is Difference between JpaRepository and CrudRepository?
CrudRepository :-
- CrudRepository provides mainly CRUD (Create, Read, Update and Delete) operation.
- The CrudRepository interface provides methods for CRUD operations, so it allows you to create, read, update and delete records without having to define your own methods.
- Interface for generic CRUD operations on a repository for a specific type.
- CrudRepository is base interface and extends the Repository interface.
public interface CrudRepository<T, ID> extends Repository<T, ID>
JpaRepository :-
- JpaRepository provides CRUD operation as well as provides JPA related methods such as flushing the persistence context and delete records in a batch.
- JpaRepository extends PagingAndSortingRepository which in turn extends CrudRepository.
- JpaRepository add some more functionality that is specific to JPA.
- The PagingAndSortingRepository provides additional methods to retrieve entities using pagination and sorting.
public interface JpaRepository<T, ID> extends PagingAndSortingRepository<T, ID>, QueryByExampleExecutor<T>
When to use CrudRepository or JpaRepository?
- CrudRepository mainly provides CRUD operations.
- PagingAndSortingRepository provides methods to do pagination and sorting data.
- JpaRepository provides some JPA related methods such as flushing the persistence context and deleting records in a batch.
Because of the above inheritance, JpaRepository will have all the functions of CrudRepository and PagingAndSortingRepository.
So use JpaRepository if you want to use methods provided by JpaRepository and PagingAndSortingRepository.
If you want to simple CRUD operation in application then use CrudRepository.
Image Source :-
https://www.javatpoint.com/spring-boot-crud-operations
HackerRank Solutions in Java :-
- Array left rotation HackerRank solution in Java
- Two Dimension DS Problem Solution in Java
- Repeated String HackerRank Solution
- Sales by Match HackerRank Solution
- Counting Valleys HackerRank Solution
Comments
Post a Comment