To configure JPA in a Spring Boot application, add spring-boot-starter-data-jpa and a database driver, set the spring.datasource.* properties, declare entities with jakarta.persistence annotations, and extend JpaRepository for each entity. Spring Boot auto-configures Hibernate, the EntityManagerFactory, the transaction manager and the connection pool, so you normally write no configuration classes.
This guide uses Spring Boot 3.x (Java 17 or later, Hibernate 6) and works the same on Java 21 and 25. It covers dependencies, datasource settings, an entity, a repository, a service and a test.
Add the starter and one driver. PostgreSQL is shown here, and H2 is common for tests.
The versions come from the Spring Boot parent or BOM, so leave them out. The Gradle equivalents are implementation 'org.springframework.boot:spring-boot-starter-data-jpa' and runtimeOnly 'org.postgresql:postgresql'. The starter brings Hibernate, Spring Data JPA, HikariCP and Spring transaction support. It does not need a separate spring-boot-starter entry.
Put the connection settings in src/main/resources/application.properties (YAML works the same way):
Points that matter:
driver-class-name is optional.spring.jpa.database-platform or a dialect for common databases. Hibernate 6 detects the dialect from the connection, and old values such as MySQL5Dialect no longer exist.ddl-auto=validate checks that the schema matches your entities. Avoid update in production and manage the schema with Flyway or Liquibase.open-in-view=false stops the persistence session from staying open during view rendering, which prevents hidden lazy loading and long connection holds. Boot enables it by default and logs a warning.For a quick local start, remove the spring.datasource.* lines. Boot then starts an embedded H2 database if H2 is on the classpath, and ddl-auto defaults to create-drop for embedded databases and none for others.
Entities use the jakarta.persistence package since Spring Boot 3. The older javax.persistence imports no longer compile.
JPA needs a no-argument constructor, which can be protected. A Java record cannot be an entity, because entities must be non-final and mutable, but records work well as DTOs and projections. Use BigDecimal for money, never double.
Spring Data creates the implementation at startup and derives queries from method names. For more complex queries use @Query with JPQL, and prefer parameter binding over string concatenation.
Returning entities from a controller is fine for a demo. In a real API return a DTO record, so the API does not change when the entity does.
Spring Boot scans from the package of the @SpringBootApplication class. If entities or repositories live in another package, add @EntityScan("com.example.domain") and @EnableJpaRepositories("com.example.repo"). Adding @EnableJpaRepositories turns off the auto-configured scanning, so list every package you need.
@DataJpaTest starts only the JPA layer with an embedded database:
Test against the real database engine for production-critical queries, using Testcontainers, because H2 differs from PostgreSQL in SQL dialect details.
Not when entities and repositories sit in the main application package or below it. Add them only for other packages.
Hibernate. The starter includes it, and Spring Data JPA works on top of the JPA API.
No. Use validate or none and manage schema changes with Flyway or Liquibase migrations.
The repository is outside the scanned packages, or the application class is in a sub-package. Move the application class to the root package or add the scan annotations.
Add the data JPA starter and a driver, set the datasource URL and credentials, write jakarta.persistence entities, and extend JpaRepository. Let Boot detect the dialect, use migrations instead of automatic schema updates, disable open-in-view, and test the repository layer with @DataJpaTest.