00:00
Free Spring Data JPA Course

Spring Data JPA Tutorial

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.

  • Repositories without boilerplate
  • Entity relationships
  • JPQL and native SQL
  • N+1 and performance

What Is Spring Data JPA?

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.

RoleRepository layer on top of JPA
Default providerHibernate
Spring Boot starterspring-boot-starter-data-jpa
Examples useSpring Boot 4 and Java 21

JPA vs Hibernate vs Spring Data JPA

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.

NameWhat it isWhat you get from it
JPA (Jakarta Persistence)A specification: annotations and interfaces@Entity, EntityManager, JPQL
HibernateAn implementation of that specificationObject-to-table mapping, SQL generation, caching
Spring Data JPAAn abstraction that uses a JPA providerGenerated repositories, derived queries, paging

What You Will Learn

  • How repository interfaces become working beans
  • Mapping entities and their relationships correctly
  • Derived queries, JPQL, native SQL and projections
  • Pagination and sorting for large tables
  • Finding and fixing the N+1 query problem
  • Transaction boundaries with @Transactional
  • Auditing changes and migrating schemas safely
  • Answering common Spring Data JPA interview questions

Who this is for

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.

Prerequisites

Core Java and OOP, basic SQL, and a little Spring Boot. New to Spring Boot? Begin with the Spring Boot tutorial.

How Spring Data JPA Works

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.

CrudRepository vs JpaRepository

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.

InterfaceWhat it adds
RepositoryA marker with no methods. You declare only what you need.
CrudRepositoryBasic create, read, update and delete. Collections come back as Iterable.
ListCrudRepositoryThe same operations, returning List.
PagingAndSortingRepositoryfindAll with Pageable or Sort. Since Spring Data 3 it no longer extends CrudRepository.
JpaRepositoryAll of the above with List results, plus JPA extras such as flush, saveAndFlush, deleteAllInBatch and getReferenceById.

Spring Data JPA Learning Path

Follow the tracks in order, or jump to the topic you need. Every link below is a published tutorial.

Common Mistakes in Production

These are the problems that experienced reviewers look for first, and they come up often in senior interviews.

N+1 Queries

Loading a list, then a relation for each row, sends one query per row. Use a fetch join or @EntityGraph.

Eager by Default

@ManyToOne and @OneToOne default to eager loading. Set them to lazy and fetch what each use case needs.

Open Session in View

Spring Boot enables it by default, which can run queries during response rendering. Set spring.jpa.open-in-view=false.

Exposing Entities

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.

Start Learning Spring Data JPA

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.