> Markdown version of [Add Spring Data JPA](https://vaadin.com/docs/next/building-apps/forms-data/persistence/add-spring-data). Section index: [llms.txt](https://vaadin.com/docs/next/building-apps/llms.txt)

# Add Spring Data JPA

[Spring Data JPA](https://spring.io/projects/spring-data-jpa) provides repository abstractions for JPA, reducing boilerplate code for common data access patterns. It builds on [Hibernate](https://hibernate.org/), the JPA implementation supported by Spring Boot.

This guide explains how to add Spring Data JPA to a Vaadin application based on Spring Boot.

If you are new to JPA, read the [Accessing Data with JPA](https://spring.io/guides/gs/accessing-data-jpa) guide before continuing.

## <a id="dependencies"></a>Dependencies

Add the Spring Data JPA starter to your `pom.xml`:

```xml
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
```

You also need a JDBC driver for your database. For example, for PostgreSQL:

```xml
<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <scope>runtime</scope>
</dependency>
```

Spring Boot declares driver versions in its parent POM, so you don’t need to specify them.

## <a id="static-metamodel-generation"></a>Static Metamodel Generation

If you intend to use the [JPA Criteria API](https://jakarta.ee/learn/docs/jakartaee-tutorial/current/persist/persistence-criteria/persistence-criteria.html) for type-safe queries, enable the Hibernate Static Metamodel Generator. This annotation processor generates `_` classes (e.g., `Customer_`) with static field references.

Add this to your `pom.xml`:

```xml
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <annotationProcessorPaths>
            <path>
                <groupId>org.hibernate.orm</groupId>
                <artifactId>hibernate-jpamodelgen</artifactId>
                <version>${hibernate.version}</version>
            </path>
        </annotationProcessorPaths>
    </configuration>
</plugin>
```

The `hibernate.version` property is inherited from the Spring Boot starter parent.

If you’re using a [multi-module project](https://vaadin.com/docs/next/building-apps/architecture/project-structure/multi-module.md), configure this only in modules containing JPA entities.

## <a id="spring-boot-configuration"></a>Spring Boot Configuration

Spring Boot auto-configures Hibernate and Spring Data JPA when the starter is on the classpath. It uses your application’s data source, which you configure in `application.properties`:

```properties
spring.datasource.url=jdbc:postgresql://localhost:5432/mydb
spring.datasource.username=myuser
spring.datasource.password=mypassword
```

The `spring.jpa.hibernate.ddl-auto` property controls what Hibernate does with the database schema on startup. Spring Boot’s default is `create-drop` for an embedded database, such as H2, when no schema migration tool is in use. Otherwise, the default is `none`, which means that Hibernate neither checks nor modifies the schema.

If you’re using [Flyway](https://vaadin.com/docs/next/building-apps/forms-data/persistence/add-flyway.md) for migrations (recommended), set it to `validate`. Hibernate then checks on startup that the entities match the database schema, but doesn’t modify it:

```properties
spring.jpa.hibernate.ddl-auto=validate
```

> **Caution: Don’t use ddl-auto in production**
>
> Avoid `create`, `create-drop`, or `update` in production. Use Flyway to manage schema changes explicitly.

## <a id="next-steps"></a>Next Steps

See [JPA & Spring Data](https://vaadin.com/docs/next/building-apps/forms-data/persistence/spring-data.md) for guidance on entities, repositories, queries, and integrating with Vaadin applications.

If you need complex queries, bulk operations, or reporting, consider combining Spring Data with [jOOQ](https://vaadin.com/docs/next/building-apps/forms-data/persistence/add-jooq.md).
