JpaRepository vs CrudRepository — Why Use JpaRepository
Fast recall
Quick revision
Core answer
Whether you know the Spring Data repository hierarchy and the concrete, practical reasons JpaRepository is the production default rather than a vague 'it has more methods'
Extending CrudRepository 'because it's simpler', then converting Iterable to List in every service method and missing flush()/batch operations when they're needed
Name the List-vs-Iterable return type and the JPA-only methods (saveAndFlush, deleteAllInBatch) as concrete reasons, and note JpaRepository is a strict superset so there's no trade-off
Interview-ready guide
Interview answer
Most Spring Data JPA applications extend JpaRepository because it places the common JPA operations in one interface. Service code gets List results, Pageable and Sort support, and explicit flush or batch-delete operations when needed. That convenience should not become a rule that every repository must expose every method. A domain-specific interface can extend Repository or copy selected signatures when callers should not have unrestricted delete or save access. Current Spring Data hierarchy details also matter: the sorting interfaces were separated from CRUD in version 3, while JpaRepository already composes the suitable list variants.
JpaRepository vs CrudRepository — Why Use JpaRepository
Direct Answer
JpaRepository is Spring Data JPA's store-specific repository interface. It combines list-based CRUD with paging and sorting and adds JPA operations such as flush, saveAndFlush, and batch deletion. It is convenient for most JPA repositories, but CrudRepository or a selective base interface can be used when a smaller public API is intentional.
Common Mistakes & Pitfalls
- Choosing CrudRepository for 'simplicity' — you then convert
IterabletoListin every service method, which is more code, not less. - Missing the JPA-only helpers —
saveAndFlush()anddeleteAllInBatch()aren't available, so bulk deletes load and delete row by row. - Assuming there's a trade-off —
JpaRepositoryis a strict superset; defaulting to it costs nothing at runtime. - Forgetting it still generates everything — you never write an implementation; Spring builds the proxy from the method names.
Check your understanding
Answer these without looking back at the article.
- What is the difference between flush() and saveAndFlush()?
- How do you implement pagination with JpaRepository?
- What is getReferenceById() and when would you use it?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.