What is the difference between JpaRepository, CrudRepository, and PagingAndSortingRepository?
Understand how JpaRepository, CrudRepository, and PagingAndSortingRepository differ in Spring Data, and learn which repository interface to pick and why.
Expected Interview Answer
They are a layered hierarchy of Spring Data interfaces: CrudRepository is the base with basic CRUD methods, PagingAndSortingRepository extends it to add pagination and sorting, and JpaRepository extends that with JPA-specific batch and flush operations.
CrudRepository<T, ID> gives you save, findById, findAll, delete, and count. PagingAndSortingRepository adds findAll(Sort) and findAll(Pageable) for ordered, paged access. JpaRepository is the richest, adding saveAll, flush, saveAndFlush, deleteInBatch, and returning List instead of Iterable, plus JPA lifecycle control. In practice most Spring Boot projects extend JpaRepository because it is a superset of the other two.
- Choose the smallest interface that fits your needs
- JpaRepository gives batch and flush control
- PagingAndSortingRepository adds pagination cleanly
- CrudRepository keeps the surface minimal
- All three auto-generate implementations at runtime
AI Mentor Explanation
Think of a cricket kit bag that grows with the level you play. CrudRepository is the club-level bag with just bat, ball, and pads for the basics. PagingAndSortingRepository adds a scoreboard so you can order and page through innings. JpaRepository is the international kit that adds everything plus batch drills and a physio, a strict superset of the smaller bags.
Step-by-Step Explanation
Step 1
Start with CrudRepository
It defines save, findById, findAll, delete, count, the minimal CRUD contract over an entity and its ID type.
Step 2
Add paging with PagingAndSortingRepository
It extends CrudRepository and adds findAll(Sort sort) and findAll(Pageable pageable) for ordered, paged reads.
Step 3
Use JpaRepository for JPA power
It extends PagingAndSortingRepository and adds saveAll, flush, saveAndFlush, deleteAllInBatch and returns List.
Step 4
Pick the right interface
Extend the smallest one that meets requirements; most apps just extend JpaRepository since it is a superset.
Step 5
Let Spring generate the implementation
At startup Spring Data creates a proxy implementing your interface, so you never write the boilerplate.
What Interviewer Expects
- Awareness that these form an inheritance hierarchy
- Knowing CrudRepository is the base
- PagingAndSortingRepository adds Pageable and Sort
- JpaRepository adds batch and flush operations
- Understanding return types differ (Iterable vs List)
Common Mistakes
- Claiming they are unrelated rather than a hierarchy
- Saying JpaRepository lacks CRUD methods
- Confusing flush with commit
- Thinking PagingAndSortingRepository has no CRUD methods
- Always defaulting to JpaRepository without knowing why
Best Answer (HR Friendly)
“These are three built-in Spring interfaces that each hand you ready-made database methods so you write less code. The most basic gives simple save-and-fetch operations, the next adds page-by-page and sorted results, and the richest adds bulk and immediate-save features. Each one builds on the one before it.”
Code Example
// Base CRUD only
interface OrderCrudRepo extends CrudRepository<Order, Long> {}
// Adds sorting + pagination
interface OrderPageRepo extends PagingAndSortingRepository<Order, Long> {}
// Full JPA power (superset of the above)
interface OrderRepository extends JpaRepository<Order, Long> {
List<Order> findByStatus(String status);
}
// Using pagination
Page<Order> page = orderRepository.findAll(PageRequest.of(0, 20, Sort.by("createdAt").descending()));
// Batch + flush only JpaRepository offers
orderRepository.saveAll(orders);
orderRepository.flush();Follow-up Questions
- How does Spring Data generate the repository implementation at runtime?
- What is the difference between flush and commit?
- How do Pageable and Sort work internally?
- When would you choose CrudRepository over JpaRepository?
- What does deleteAllInBatch do differently from deleteAll?
MCQ Practice
1. Which interface sits at the base of the Spring Data hierarchy?
CrudRepository defines the core CRUD methods; the other two extend it directly or transitively.
2. Which method is uniquely provided by JpaRepository?
saveAndFlush and other batch/flush operations are added by JpaRepository, not the base interfaces.
3. What does PagingAndSortingRepository add over CrudRepository?
It adds sorted and paged findAll variants while inheriting all CRUD methods.
Flash Cards
What does CrudRepository provide? — Basic CRUD: save, findById, findAll, delete, count over an entity and ID type.
What does PagingAndSortingRepository add? — findAll(Sort) and findAll(Pageable) for ordered and paginated reads.
What does JpaRepository add? — Batch and flush ops (saveAll, flush, saveAndFlush, deleteAllInBatch) and List return types.
Which is a superset of all three? — JpaRepository, it transitively extends both other interfaces.