Foundations
What Spring Data JPA is, and how it builds on JPA and Hibernate.
Spring Data JPA is a Spring library that removes most of the repetitive code from the data layer of a Java application. You declare a repository interface, and Spring generates the implementation that stores and reads your entities through JPA. This Spring Data JPA tutorial takes you from your first repository to fast, interview-ready persistence code.
Spring Data JPA is the part of the Spring Data project that adds a repository layer on top of JPA. Saving an entity, finding it by id or paging through a table no longer needs hand-written EntityManager code, because you inherit those operations from an interface. Your own lookups can be declared just by naming a method, such as findByTrackingCode.
These three names are often mixed up, and interviewers like to check that you can separate them. Each one sits on top of the next.
| Name | What it is | What you get from it |
|---|---|---|
| JPA (Jakarta Persistence) | A specification: annotations and interfaces | @Entity, EntityManager, JPQL |
| Hibernate | An implementation of that specification | Object-to-table mapping, SQL generation, caching |
| Spring Data JPA | An abstraction that uses a JPA provider | Generated repositories, derived queries, paging |
Java developers meeting persistence for the first time, Spring Boot and microservices engineers with 3 to 15 years of experience, and anyone preparing for product-company interviews.
Core Java and OOP, basic SQL, and a little Spring Boot. New to Spring Boot? Begin with the Spring Boot tutorial.
At startup, Spring finds your repository interfaces and creates a proxy for each one. Standard calls such as save go to a built-in class, SimpleJpaRepository. Your own query methods are parsed or read from @Query, so a method name that does not match the entity fails when the application starts, not in production. Everything ends up at the EntityManager, where Hibernate produces the SQL.
Spring Data offers several repository interfaces. JpaRepository is the usual choice for JPA projects because it includes the others' features. Pick a narrower interface when you want to expose fewer operations.
| Interface | What it adds |
|---|---|
Repository | A marker with no methods. You declare only what you need. |
CrudRepository | Basic create, read, update and delete. Collections come back as Iterable. |
ListCrudRepository | The same operations, returning List. |
PagingAndSortingRepository | findAll with Pageable or Sort. Since Spring Data 3 it no longer extends CrudRepository. |
JpaRepository | All of the above with List results, plus JPA extras such as flush, saveAndFlush, deleteAllInBatch and getReferenceById. |
Follow the tracks in order, or jump to the topic you need. Every link below is a published tutorial.
What Spring Data JPA is, and how it builds on JPA and Hibernate.
Model relationships correctly and track who changed what.
Write the queries that derived method names cannot express, and page through results.
Find and remove the extra queries that slow an application down.
Keep data consistent and change your schema safely.
Apply the same repository model to other data stores.
These are the problems that experienced reviewers look for first, and they come up often in senior interviews.
Loading a list, then a relation for each row, sends one query per row. Use a fetch join or @EntityGraph.
@ManyToOne and @OneToOne default to eager loading. Set them to lazy and fetch what each use case needs.
Spring Boot enables it by default, which can run queries during response rendering. Set spring.jpa.open-in-view=false.
Returning entities from a controller couples your API to the schema and can trigger lazy loads. Return DTOs or records.
For interview practice, work through the Spring Boot interview questions after completing the tracks.
Spring Data JPA lets you spend your time on business rules instead of data access plumbing, but it still rewards knowing what happens underneath. Work through this Spring Data JPA tutorial track by track, starting with the overview, and pay special attention to fetch strategies and transactions, where most production issues begin.