Video in progress

Junior 🌱 Spring

What is the difference between CrudRepository, PagingAndSortingRepository and JpaRepository?

What it adds — CrudRepository: save, findById, findAll, deleteById, count, existsById; ListCrudRepository: the same, but returns List instead of Iterable; PagingAndSortingRepository: findAll(Pageable), findAll(Sort); JpaRepository: flush, saveAndFlush, deleteAllInBatch, getReferenceById and all of the above

📖 Answer

Interface What it adds
CrudRepository save, findById, findAll, deleteById, count, existsById
ListCrudRepository the same, but returns List instead of Iterable
PagingAndSortingRepository findAll(Pageable), findAll(Sort)
JpaRepository flush, saveAndFlush, deleteAllInBatch, getReferenceById and all of the above

In Spring Data 3 (Boot 3) one thing changed: PagingAndSortingRepository no longer extends CrudRepository. If you stay with only that, the save method is gone.

public interface ProductRepository extends JpaRepository<Product, Long> {
    List<Product> findByCategory(String category);
}

In practice, almost everyone in a JPA project takes JpaRepository. If you only need reads, or you may later move to another storage (say, MongoDB), you can extend Repository and declare only the methods you need.