Add jOOQ
jOOQ is a framework for writing SQL queries in Java. It analyzes your database schema and generates Java classes that let you build type-safe SQL queries using a fluent API.
This guide explains how to add jOOQ to a Vaadin application based on Spring Boot. You can use jOOQ without Spring Boot, but in that case you must configure the integration manually.
If you are new to jOOQ, read the jOOQ tutorial before continuing.
Java Code Generation
To get the most out of jOOQ, you should configure it to generate Java code from your database schema. The code generator analyzes the schema and reverse-engineers it into classes that model tables, records, sequences, and other database objects.
In a Vaadin application, the easiest approach is to declare your schema using Flyway migrations and configure jOOQ to parse them using the DDLDatabase. For alternative approaches, see the jOOQ documentation.
The jOOQ code generator is executed using a Maven plugin. Add the following plugin configuration to your pom.xml file:
Source code
XML
<plugin>
<groupId>org.jooq</groupId>
<artifactId>jooq-codegen-maven</artifactId>
<version>${jooq.version}</version> 1
<executions>
<execution>
<id>jooq-codegen</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<generator>
<database>
<name>org.jooq.meta.extensions.ddl.DDLDatabase</name>
<properties>
<property>
<key>scripts</key>
<value>src/main/resources/db/migration/*.sql</value> 2
</property>
<property>
<key>sort</key>
<value>flyway</value> 3
</property>
</properties>
</database>
<target>
<packageName>com.example.application.jooq</packageName> 4
</target>
</generator>
</configuration>
<dependencies>
<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq-meta-extensions</artifactId>
<version>${jooq.version}</version>
</dependency>
</dependencies>
</plugin>-
The
jooq.versionproperty is inherited from the Spring Boot starter parent. -
Configures the plugin to read SQL scripts from the Flyway migrations directory.
-
Ensures that scripts are processed in the same order as Flyway applies them.
-
The Java package into which the jOOQ classes are generated.
The code generator runs automatically whenever you build the Maven module. You can also trigger generation manually by running:
Source code
terminal
$ mvn jooq-codegen:generateIf you’re using a multi-module project, configure the plugin in the module that contains the Flyway migrations.
Spring Boot Configuration
To use jOOQ at runtime, add the Spring Boot jOOQ starter dependency to your project:
Source code
XML
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jooq</artifactId>
</dependency>This starter automatically configures jOOQ to use the application’s primary data source and integrates it with Spring’s transaction management. As a result, you can manage transactions consistently whether you use jOOQ or JPA.
For more information about transaction management in Vaadin applications, see the Transactions documentation.
Once configured, you can inject org.jooq.DSLContext into any Spring-managed bean that needs to execute jOOQ queries.
Next Steps
See jOOQ for guidance on queries, active records, repositories, and integrating with Vaadin applications.
If you need aggregate-oriented persistence with automatic change tracking, consider combining jOOQ with Spring Data JPA.