6.3. jOOQ Code Generation
6.3.1. Overview
jOOQ automatically generates type-safe Java classes from the database schema. After applying Flyway migrations, jOOQ codegen is executed to keep the schema and code always in sync.
Flyway migration execution → DB schema applied → jOOQ codegen → Java classes generated6.3.2. Maven Plugin Configuration
xml
<plugin>
<groupId>org.jooq</groupId>
<artifactId>jooq-codegen-maven</artifactId>
<version>${jooq.version}</version>
<executions>
<execution>
<id>jooq-codegen</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<jdbc>
<driver>org.postgresql.Driver</driver>
<url>jdbc:postgresql://localhost:5432/flowin</url>
<user>flowin</user>
<password>flowin1234</password>
</jdbc>
<generator>
<database>
<name>org.jooq.meta.postgres.PostgresDatabase</name>
<inputSchema>public</inputSchema>
<includes>.*</includes>
<excludes>flyway_schema_history</excludes>
</database>
<target>
<packageName>com.tienipia.flowin.jooq</packageName>
<directory>target/generated-sources/jooq</directory>
</target>
<generate>
<records>true</records>
<daos>false</daos>
<pojos>false</pojos>
<fluentSetters>true</fluentSetters>
<javaTimeTypes>true</javaTimeTypes>
</generate>
</generator>
</configuration>
<dependencies>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>${postgresql.version}</version>
</dependency>
</dependencies>
</plugin>6.3.3. Code Generation Configuration Principles
| Setting | Value | Reason |
|---|---|---|
records | true | Generate Record classes (used for CRUD operations) |
daos | false | Do not auto-generate DAOs (write Repositories manually) |
pojos | false | Do not use POJOs (use separate DTOs) |
fluentSetters | true | Support method chaining |
javaTimeTypes | true | Use java.time types (instead of Timestamp) |
excludes | flyway_schema_history | Exclude Flyway internal tables |
6.3.4. Generated Code Management
- Output location:
target/generated-sources/jooq/ - Generated code must not be committed to Git. It is always regenerated during the build.
- Include the following in
.gitignore:
txt
target/generated-sources/6.3.5. Generated Code Structure
target/generated-sources/jooq/
└── com/tienipia/flowin/jooq/
├── DefaultCatalog.java
├── Public.java # Schema reference
├── Keys.java # PK, FK definitions
├── Tables.java # Table constants
└── tables/
├── Users.java # Table definition
├── Orders.java
└── records/
├── UsersRecord.java # Record classes
└── OrdersRecord.java