Sub topic 2
Setting up Spring Data JPA in a Spring Boot project
This chapter assumes you can build a Spring Boot application with Maven or Gradle. It also assumes you know what a bean is and how application.properties works. By the end you will have a project that starts a JPA layer against an in-memory database.
What the starter gives you
One dependency, spring-boot-starter-data-jpa, brings in Spring Data JPA, Hibernate as the JPA provider, the transaction support and a connection pool (HikariCP). You also need a JDBC driver. H2 is a good choice for learning because it runs inside the JVM.
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>What auto-configuration does
When Boot finds these classes on the classpath, it builds the pieces for you.
Boot scans for @Entity classes and repository interfaces. It starts the scan in the package of your @SpringBootApplication class and continues into its subpackages. Entities or repositories outside that package are not found unless you add @EntityScan and @EnableJpaRepositories.
package com.example.catalog;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class CatalogApplication {
public static void main(String[] args) {
SpringApplication.run(CatalogApplication.class, args);
}
}Configuration
Put these properties in src/main/resources/application.properties.
spring.datasource.url=jdbc:h2:mem:catalog
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql=true
spring.jpa.open-in-view=falseThe JPA properties do the following:
| Property | Effect |
|---|---|
| `spring.jpa.hibernate.ddl-auto` | Lets Hibernate create or validate the schema. `create-drop` builds the tables at startup and removes them at shutdown. |
| `spring.jpa.show-sql` | Prints each SQL statement. Useful while learning. |
| `spring.jpa.open-in-view` | When `true` (the default), the persistence context stays open during web rendering. Setting it to `false` keeps database access inside your service layer. |
Common setup errors
Not a managed type: The entity is outside the scanned packages, or it lacks@Entity.Failed to determine a suitable driver class: No JDBC driver is on the classpath, orspring.datasource.urlis missing.No qualifying bean of type ...Repository: The repository interface is outside the scanned packages.- Imports from
javax.persistence: Spring Boot 3 and later usejakarta.persistence. Older tutorials show the wrong package.
Exercise
Your @Entity classes live in com.example.model, but your @SpringBootApplication class is in com.example.catalog. The application fails with "Not a managed type". Which annotation fixes this, and what attribute does it need?
Show answer
Add @EntityScan(basePackages = "com.example.model") to a configuration class or the application class. Repositories in a package outside com.example.catalog would also need @EnableJpaRepositories with its own basePackages.