Add Spring Data JPA
Spring Data JPA provides repository abstractions for JPA, reducing boilerplate code for common data access patterns. It builds on Hibernate, 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 guide before continuing.
Dependencies
Add the Spring Data JPA starter to your pom.xml:
Source code
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:
Source code
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.
Static Metamodel Generation
If you intend to use the JPA Criteria API 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:
Source code
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, configure this only in modules containing JPA entities.
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:
Source code
properties
spring.datasource.url=jdbc:postgresql://localhost:5432/mydb
spring.datasource.username=myuser
spring.datasource.password=mypasswordBy default, Hibernate validates that entities match the database schema but doesn’t modify it. If you’re using Flyway for migrations (recommended), keep this default:
Source code
properties
spring.jpa.hibernate.ddl-auto=validate|
Caution
|
Don’t use
Avoid ddl-auto in productioncreate, create-drop, or update in production. Use Flyway to manage schema changes explicitly.
|
Next Steps
See JPA & Spring Data 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.