JpaRepository vs CrudRepository vs PagingAndSortingRepository
Fast recall
Quick revision
Core answer
save, findById, findAll, delete). `PagingAndSortingRepositoryCrudRepository) adds findAll(Pageable) and findAll(Sort). `JpaRepositoryflush(), saveAllAndFlush(), getReferenceById(), deleteAllInBatch()). For most JPA apps, extend `JpaRepository`. Extend CrudRepository (with separate PagingAndSortingRepository if needed) when you want a smaller surface area, persistence-tech portability, or do not need JPA-specific methods.Whether the candidate knows the Spring Data interface hierarchy and chooses deliberately rather than always extending JpaRepository
Extending JpaRepository everywhere without realizing it exposes JPA-specific operations and ties the repository contract to JPA forever
Mention the Spring Data 3 split (PagingAndSortingRepository no longer extends CrudRepository), and explain when `getReferenceById` (lazy proxy) beats `findById`
Interview-ready guide
Interview answer
Spring Data ships a hierarchy of repository interfaces.
The hierarchy:
Repository— empty marker; activates Spring DataCrudRepository— basic CRUDListCrudRepository— same but returnsListinstead ofIterablePagingAndSortingRepository— paging and sorting; in Spring Data 3 it is separate from CrudRepositoryJpaRepository— combines CRUD + paging + JPA-specific (flush,saveAndFlush,getReferenceById,deleteAllInBatch)
Which to extend:
- Standard JPA app ->
JpaRepository - Library code or persistence-tech neutral ->
CrudRepository/ListCrudRepository - Read-only repository with paging ->
PagingAndSortingRepository - Maximum control over the API surface -> extend
Repositorydirectly and re-declare the methods you want
JPA-specific methods worth mentioning:
getReferenceById— returns a Hibernate proxy; useful for setting a FK without loading the parentsaveAndFlush— forces immediate flush; useful when subsequent code needs DB-side statedeleteAllInBatch— single DELETE; skips cascade and callbacks; raw bulk cleanup
Spring Data 3 change: PagingAndSortingRepository no longer extends CrudRepository — you can choose paging without CRUD, useful for read-only repositories.
JpaRepository vs CrudRepository vs PagingAndSortingRepository
Direct Answer
Spring Data defines a hierarchy of repository interfaces — pick the one whose contract matches your need. Repository<T, ID> is the empty marker interface enabling Spring Data scanning. CrudRepository<T, ID> adds CRUD methods (save, findById, findAll, delete). PagingAndSortingRepository<T, ID> (since Spring Data 3 it is separate from CrudRepository) adds findAll(Pageable) and findAll(Sort). JpaRepository<T, ID> combines CRUD + paging + JPA-specific methods (flush(), saveAllAndFlush(), getReferenceById(), deleteAllInBatch()). For most JPA apps, extend JpaRepository. Extend CrudRepository (with separate PagingAndSortingRepository if needed) when you want a smaller surface area, persistence-tech portability, or do not need JPA-specific methods.
Code Example
Standard repository — extends JpaRepository:
public interface OrderRepository extends JpaRepository<Order, Long> {
List<Order> findByStatus(OrderStatus status);
}
Narrow API — only what you want exposed:
public interface OrderLookup extends Repository<Order, Long> {
Optional<Order> findById(Long id);
Order save(Order order);
}
No deleteAll(), no findAll() — callers cannot misuse the repository.
Read-only paging repository:
public interface OrderReportRepository
extends ListPagingAndSortingRepository<Order, Long> {
Page<Order> findByCreatedAtBetween(Instant from, Instant to, Pageable page);
}
No writes possible.
Persistence-neutral domain repository:
public interface CustomerRepository extends ListCrudRepository<Customer, UUID> {
Optional<Customer> findByEmail(String email);
}
Can later swap from JPA to MongoDB / R2DBC by changing only the implementation tech.
Common Mistakes & Pitfalls
Extending JpaRepository everywhere without realizing it exposes JPA-specific operations and ties the repository contract to JPA forever
Check your understanding
Answer these without looking back at the article.
- When would you use `getReferenceById` instead of `findById`?
- What is the difference between `deleteAll()` and `deleteAllInBatch()`?
- How do you restrict the public API of a repository so consumers cannot call `deleteAll`?
- Why did Spring Data 3 split `PagingAndSortingRepository` from `CrudRepository`?
Related interview prep categories
2Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.