Spring Boot Starter Data JPA is the starter that sets up relational database access in a Spring Boot application. It combines Spring Data JPA, Hibernate as the default JPA provider, a connection pool and transaction support, so you can define a repository interface and get working CRUD methods without writing implementations.
This article is a practical walkthrough: add the starter, configure a database, map an entity, create a repository and call it from a service.
Add the starter and the JDBC driver for your database. For PostgreSQL with Maven:
The starter does not include a driver, because it cannot know which database you use. For tests you can add H2 instead.
Two settings deserve attention:
spring.jpa.hibernate.ddl-auto: update and create are convenient while prototyping, but they are unsafe in production because they change the schema automatically. Use validate or none and manage the schema with a migration tool such as Flyway or Liquibase.spring.jpa.open-in-view: it defaults to true, which keeps the persistence session open during view rendering and logs a warning. Setting it to false is recommended for APIs so that lazy loading outside a service transaction fails clearly instead of hiding extra queries.The older example of putting a database password in plain text in application.properties is fine for a local demo only; use environment variables or a secret manager elsewhere.
In Spring Boot 3 and later, JPA classes live in the jakarta.persistence package (older code used javax.persistence).
An entity needs a no-argument constructor (it can be protected) and an @Id. Records cannot be entities, though they work well as projections.
Spring Data creates the implementation at startup. Methods such as save, findById, findAll, deleteById and count come from JpaRepository, and the derived queries findByUsername and existsByEmail are generated from their names. Use CrudRepository if you want a smaller interface, or PagingAndSortingRepository features through Pageable parameters.
For queries that names cannot express, add @Query:
Put @Transactional on service methods, not on repositories, so that several repository calls commit or roll back together. Constructor injection, as shown, is the recommended style.
Return a Page for list endpoints instead of loading a whole table. When you need only some columns, declare an interface or a record projection:
Not for common cases. Derived queries cover most lookups, @Query accepts JPQL or native SQL for the rest.
It does not pick one. If an embedded database such as H2 is on the classpath and no URL is set, Spring Boot configures it. Otherwise you must set spring.datasource.url and add a driver.
JpaRepository adds paging, flushing and batch operations on top of CrudRepository, so it is the usual choice for JPA applications.
You accessed a lazy association after the transaction ended. Load the data inside the service method, use a fetch join or an entity graph, or return a projection.
Add spring-boot-starter-data-jpa and a database driver, configure spring.datasource.*, map entities with jakarta.persistence annotations and extend JpaRepository. Use validate with a migration tool instead of ddl-auto=update in production, disable open-in-view for APIs and keep transactions in the service layer.