Quick revision
The short answer
Repository is an empty marker interface; CrudRepository adds the basic create, read, update and delete methods; ListCrudRepository returns List instead of Iterable; PagingAndSortingRepository adds paging and sorting but, since Spring Data 3.0, no longer extends CrudRepository; and JpaRepository combines them and adds JPA-specific methods such as flush, saveAndFlush, deleteAllInBatch and getReferenceById. At startup, Spring Data creates a proxy for each repository interface, backed by SimpleJpaRepository for the inherited methods and by generated queries for the methods we declare.
What to remember
In this answer, we saw that Repository is a marker, CrudRepository and ListCrudRepository add the basic operations, PagingAndSortingRepository adds paging and since 3.0 stands on its own, and JpaRepository brings them together with JPA-specific methods. At startup Spring Data builds a proxy for each interface, sending inherited calls to SimpleJpaRepository and running queries it prepared from our method names or @Query annotations, and @NoRepositoryBean keeps shared base interfaces from being instantiated.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Repository is an empty marker interface; CrudRepository adds the basic create, read, update and delete methods; ListCrudRepository returns List instead of Iterable; PagingAndSortingRepository adds paging and sorting but, since Spring Data 3.0, no longer extends CrudRepository; and JpaRepository combines them and adds JPA-specific methods such as flush, saveAndFlush, deleteAllInBatch and getReferenceById. At startup, Spring Data creates a proxy for each repository interface, backed by SimpleJpaRepository for the inherited methods and by generated queries for the methods we declare.
Why this question is asked
Almost every Spring Boot service that talks to a relational database has interfaces like OrderRepository extends JpaRepository<Order, Long>, and most of us wrote our first one without knowing where its implementation comes from. Interviewers ask about the hierarchy because choosing the right base interface shapes what other code can do with our data, and because the 3.0 changes caught many teams during upgrades. The second half of the question, how an interface gets an implementation, tells them whether we understand proxies and query derivation well enough to debug them. In this answer, we will walk down the hierarchy one interface at a time, note what changed in Spring Data 3.0, see how Spring builds the implementation at startup, and finish with @NoRepositoryBean for shared base interfaces.
1A Quick Recap: What a Repository Is
A repository, in the sense Spring Data uses, is an object that looks like a collection of entities of one type. We ask it to save an order, find an order by id or list all paid orders, and it turns those requests into database calls. An entity is a class mapped to a table, such as Order mapped to the orders table.
Spring Data's idea is that we only write the interface. We say which entity it manages and what type the id has, through the two generic parameters, and Spring Data supplies the code. Each interface in the hierarchy adds a group of methods, so the one we extend decides which methods our repository offers.
2Walking Down the Hierarchy
Repository<T, ID> has no methods at all. It is a marker: it tells Spring Data that this interface is a repository for entity T with id type ID. If we extend only Repository, our repository offers exactly the methods we declare and nothing else, which is useful when we want a read-only or very narrow repository.
CrudRepository<T, ID> adds the familiar methods: save, saveAll, findById, existsById, findAll, findAllById, count, deleteById, delete, deleteAll. The methods that return many entities return Iterable<T>, which is the most general collection type but awkward to use, because we often have to convert it to a List.
ListCrudRepository<T, ID>, added in Spring Data 3.0, offers the same methods but returns List<T> from findAll, findAllById and saveAll. It exists only to remove that conversion. There is a matching ListPagingAndSortingRepository as well.
PagingAndSortingRepository<T, ID> adds findAll(Sort) and findAll(Pageable). The important change in Spring Data 3.0 is that it no longer extends CrudRepository. Before 3.0, extending PagingAndSortingRepository gave us save and delete too; since 3.0 it gives only the paging methods, so code that relied on the old inheritance stops compiling after an upgrade. The fix is to extend both interfaces, or to extend JpaRepository.
JpaRepository<T, ID> extends ListCrudRepository, ListPagingAndSortingRepository and QueryByExampleExecutor, and adds methods that only make sense with JPA: flush to push pending changes to the database now, saveAndFlush and saveAllAndFlush, deleteAllInBatch and deleteAllByIdInBatch which issue a single delete statement, and getReferenceById, which returns a lazy reference without loading the row.
| Interface | Extends | Adds |
|---|---|---|
| Repository | nothing | No methods; marks the interface as a repository |
CrudRepository | Repository | save, findById, findAll, count, delete... (Iterable) |
ListCrudRepository | CrudRepository | Same methods returning List (3.0+) |
PagingAndSortingRepository | Repository (since 3.0) | findAll(Sort), findAll(Pageable) |
JpaRepository | ListCrud, ListPagingAndSorting, QBE | flush, saveAndFlush, deleteAllInBatch, getReferenceById |
JpaRepository joins.3Seeing the Hierarchy in Code
Here is a repository that extends JpaRepository and declares two methods of its own, plus a narrow read-only repository that extends only Repository.
Notice that OrderReadRepository can declare findById with the same signature as CrudRepository, and Spring Data still implements it. We can pick individual CRUD methods without inheriting the rest.
4How the Interface Gets an Implementation
When the application starts, Spring Boot's auto-configuration enables JPA repositories and scans the application's packages for interfaces that extend Repository. For each one, a repository factory bean builds a JDK dynamic proxy: an object, created at runtime, that implements our interface and forwards each call to a handler.
For the inherited methods, the handler forwards the call to a SimpleJpaRepository instance, the real class in Spring Data JPA that implements JpaRepository using an EntityManager. That is where save, findById and the others are actually written, and it is also where their @Transactional annotations live.
For the methods we declared ourselves, Spring Data looks at each one when the proxy is built. If it has @Query, Spring Data prepares that query. If not, it parses the method name, findByStatus becomes a query on the status property, and prepares a query from it. Because this happens at startup, a method name that refers to a property that does not exist fails the application start rather than the first request. Custom fragment implementations, classes we write ourselves, are also combined into the same proxy.
7 messages in order. Play them, or step through them one at a time.
Inherited methods go to SimpleJpaRepository; declared methods run queries prepared at startup.
5Shared Base Interfaces and @NoRepositoryBean
Sometimes we want our own base interface, for example one that all repositories in a module extend, with a method such as findByIdOrThrow. If we just write interface BaseRepository<T, ID> extends JpaRepository<T, ID>, Spring Data finds it during scanning and tries to create a repository for it. That fails, because T is not a real entity type.
Annotating it with @NoRepositoryBean tells Spring Data that this interface is a building block, not a repository to instantiate. Spring Data itself uses the same annotation on CrudRepository, JpaRepository and the rest, which is why those are never turned into beans.
6Follow-up questions
Which interface should a new repository extend?
For most JPA services, JpaRepository is fine and convenient. If we want to limit what callers can do, for example a read-only repository, extending Repository and declaring only the methods we need is cleaner, because it makes the allowed operations explicit.
Why did my upgrade to Spring Boot 3 break code that called save on a PagingAndSortingRepository?
Because in Spring Data 3.0 PagingAndSortingRepository stopped extending CrudRepository, so it no longer has save or delete. Extend CrudRepository or ListCrudRepository as well, or extend JpaRepository, which includes both branches.
Is getReferenceById the same as findById?
No. findById runs a select and returns Optional of a fully loaded entity, or empty. getReferenceById returns a lazy proxy without querying, and only hits the database when we read a field; if the row does not exist, that first access throws EntityNotFoundException. It is useful for setting a foreign key without loading the related row.
7Conclusion
In this answer, we saw that Repository is a marker, CrudRepository and ListCrudRepository add the basic operations, PagingAndSortingRepository adds paging and since 3.0 stands on its own, and JpaRepository brings them together with JPA-specific methods. At startup Spring Data builds a proxy for each interface, sending inherited calls to SimpleJpaRepository and running queries it prepared from our method names or @Query annotations, and @NoRepositoryBean keeps shared base interfaces from being instantiated.
Open with
The hierarchy starts with Repository, which is just a marker with no methods. It tells Spring Data the entity type and id type. CrudRepository adds the basic operations like save, findById, findAll, count and deleteById, returning Iterable for collections.
Then, if they want more
Spring Data 3.0 added ListCrudRepository, which is the same but returns List, so we do not have to convert. PagingAndSortingRepository adds findAll with Sort and Pageable, and one thing to know is that since 3.0 it no longer extends CrudRepository, which broke some code during Boot 3 upgrades.
JpaRepository sits on top: it combines the list-based CRUD and paging interfaces and adds JPA-specific methods like flush, saveAndFlush, deleteAllInBatch and getReferenceById, which gives a lazy reference without a select. As for the implementation, we never write one. At startup Spring Data scans for interfaces extending Repository and, for each one, builds a dynamic proxy.
Calls to inherited methods are forwarded to SimpleJpaRepository, which implements them with the EntityManager and carries their transaction settings. For the methods I declare, Spring Data either uses the @Query I gave or parses the method name into a query, and it does this at startup, so a misspelled property fails the boot rather than a request.
If I want a shared base interface with helper methods, I annotate it with @NoRepositoryBean so Spring Data does not try to create a repository for it.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Data JPA reference, Hibernate ORM documentation, Spring Framework reference, Transaction management
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Explain the Spring Data repository hierarchy (Repository, CrudRepository...": https://scrivio-interview-library.netlify.app/databases/spring-data-jpa/112b9076c8/