A JPA repository is an interface that Spring Data JPA turns into a working data access object at runtime. You extend JpaRepository<T, ID>, declare the entity and its ID type, and Spring generates CRUD, paging, sorting and query methods without any implementation code.
The repository pattern keeps persistence logic out of your services: business code calls userRepository.findById(id) and does not care whether the data comes from PostgreSQL, MySQL or another database. The examples below use Spring Boot 3 and Jakarta Persistence (jakarta.persistence).
JPA (Jakarta Persistence) is the specification for mapping Java objects to relational tables. Hibernate is the usual implementation. Spring Data JPA sits on top and removes the boilerplate of writing EntityManager code for every entity.
At startup Spring scans for interfaces that extend a Repository type and creates a proxy for each one. The proxy implements the standard methods using the EntityManager and parses your custom method names or annotations into queries.
Start with an entity.
Then declare the repository. No class implements it and no annotation is needed, because Spring Boot auto-configures repository scanning.
Add the spring-boot-starter-data-jpa dependency and a JDBC driver, and set spring.datasource.* properties to connect.
Spring Data offers several base interfaces, each adding more features.
| Interface | Adds |
|---|---|
Repository<T, ID> | Marker only, no methods |
CrudRepository<T, ID> | save, findById, findAll, deleteById, count, existsById |
ListCrudRepository<T, ID> | Same as CRUD but returns List instead of Iterable |
PagingAndSortingRepository<T, ID> | findAll(Pageable) and findAll(Sort) |
JpaRepository<T, ID> | JPA extras such as flush, saveAndFlush and batch deletes |
Extend the smallest interface that gives you what you need. If you want to hide delete operations, extend Repository and copy only the methods you want to expose.
save performs an insert for a new entity and a merge for an existing one. findById returns an Optional, so you handle the missing case explicitly.
Spring parses the method name and builds the query. Parameters must follow the order of the conditions in the name.
When a name becomes unreadable, write the JPQL yourself, using named parameters.
Set nativeQuery = true for plain SQL. For modifying queries add @Modifying, and call them inside a transaction.
A Page includes the total count, which costs an extra query. Return Slice when you only need to know whether a next page exists.
@EntityGraph or join fetch when you load a collection with its parent.@Query or the Specification API.@DataJpaTest, ideally against the same database engine using Testcontainers instead of an in-memory substitute.spring.jpa.open-in-view=false to avoid holding a database connection while rendering responses.CrudRepository offers basic create, read, update and delete methods. JpaRepository extends it with paging, sorting and JPA-specific methods such as flush and deleteAllInBatch, and returns List instead of Iterable.
No. Spring Data detects interfaces that extend its repository types and registers them as beans. The annotation is only needed for your own hand-written repository classes, where it also enables exception translation.
Create a custom fragment interface and implementation class, then make the repository extend the fragment. Spring Data merges the implementation into the generated proxy.
No. For an entity with a null ID (or one detected as new) it persists; otherwise it merges, which usually loads the entity first and then updates it.
A JPA repository is an interface that Spring Data JPA implements for you. Extend JpaRepository for CRUD, paging and sorting, add derived query methods or @Query for custom lookups, and keep transactions in your services. Use projections, @EntityGraph and @DataJpaTest to keep it fast and tested.