Difference between CrudRepository and JpaRepository in Spring Data JPA

In this post, we will see the difference between CrudRepository and JpaRepository in Spring Data JPA. First, we will see all points in brief later will see all points in details.

CrudRepository JpaRepository
1. CrudRepository extends Repository interface. 1. JpaRepository extends PagingAndSortingRepository and QueryByExampleExecutor interface.
2. CrudRepository provides methods to perform CRUD operations. 2. JpaRepository provides additional methods like flush(), saveAndFlush(), deleteInBatch() etc.
3. The saveAll(Iterable<S> entities)  method of CrudRepository returns Iterable. 3. The saveAll(Iterable<S> entities)  method of JpaRepository returns List.
4. If we have to perform mainly CRUD operation, define our repository using CrudRepository. 4. If we have to perform CRUD as well as Batch operation define our repository extending JpaRepository.

Difference between CrudRepository and JpaRepository in details.

Let’s see all points in details.

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

 

Difference between CrudRepository and JpaRepository in Spring Data JPA

Let’s see some more details about the Difference between CrudRepository and JpaRepository.

The methods of Crudrepository and JpaRepository interfaces.

Crudrepository interface methods.

<S extends T> S save(S entity) –  See more details about save() method here.

<S extends T> Iterable<S> saveAll(Iterable<S> entities) – See more details about saveAll() method here.

Optional<T> findById(ID id) – See more details about findById() method here.

Iterable<T> findAll() – See more details about findAll() method here.

Iterable<T> findAllById(Iterable<ID> ids) – See more details about findAllById() method here.

long count() – See more details about count() method here.

void deleteById(ID id) – See more details about deleteById() method here.

void delete(T entity) – See more details about delete() method here.

void deleteAll(Iterable<? extends T> entities) – See more details about deleteAll() method here.

void deleteAll() – See more details about deleteAll() method here.

JpaRepository interface methods.

<S extends T> List<S> saveAll(Iterable<S> entities) – See more details about saveAll() method here.

void flush().

<S extends T> S saveAndFlush(S entity) – See more details about saveAndFlush() method here.

void deleteInBatch(Iterable<T> entities) – See more details about deleteInBatch() method here.

void deleteAllInBatch() – See more details about deleteAllInBatch() method here.

T getOne(ID id) – See more details about getOne() method here.

<S extends T> List<S> findAll(Example<S> example).

<S extends T> List<S> findAll(Example<S> example, Sort sort).

 

For the third point observe return type of saveAll() of both interfaces. The return type of saveAll() defined in CrudRepository is Iterable whereas return type of saveAll() defined in JpaRepository is List.

CrudRepository – <S extends T> Iterable<S> saveAll(Iterable<S> entities).

JpaRepository  – <S extends T> List<S> saveAll(Iterable<S> entities)

 

Creating a custom repository extending CrudRepository interface.

Creating a custom repository extending JpaRepository interface.

 

 

That’s all about Difference between CrudRepository and JpaRepository in Spring Data JPA.

You may like.

Other Spring Data JPA and Hibernate post.

Spring Data JPA Docs.

Summary – We have seen Difference between CrudRepository and JpaRepository in Spring Data JPA.

 

Summary – Difference between CrudRepository and JpaRepository.

Top