With jOOQ's code generator, which classes does it write for each table, and which outputs stay off unless you enable them?
answer
- one class per table, one per row
- schema-wide constant holders
- records on, POJOs off
- DAOs pull in records and POJOs
basics
~20 sFor each table, jOOQ's code generator writes a table class with typed column fields and a record class for its rows, plus schema-wide Tables and Keys classes. POJOs, DAOs and interfaces are off by default.
solid answer
~30 sFor a `PAYMENT` table, jOOQ's default generator writes `tables.Payment`, a table class with a static `PAYMENT` instance and one typed `TableField` per column, and `tables.records.PaymentRecord`, which extends `UpdatableRecordImpl` when the table has a primary key and `TableRecordImpl` when it has none. Around them it writes schema-wide classes: `Tables`, `Keys`, `Indexes`, a schema class and `DefaultCatalog`. The `<generate>` flags for tables, records, keys, indexes, sequences and routines default to `true`; `pojos`, `daos`, `interfaces` and `immutablePojos` default to `false`. Turning on `daos` also turns on records and POJOs, because a generated DAO is built from both.
code
xml · 11 lines<generator>
<name>org.jooq.codegen.DefaultGenerator</name>
<generate>
<pojos>true</pojos>
<pojosAsJavaRecordClasses>true</pojosAsJavaRecordClasses>
<daos>false</daos>
</generate>
<target>
<packageName>com.example.payments.db</packageName>
</target>
</generator>go deeper
Recall the two per-table outputs, the table class with typed fields and the record class, and that POJOs and DAOs must be switched on.
Explain the schema-wide holders (Tables, Keys, DefaultCatalog), the default flags, and why enabling daos quietly turns on records and POJOs too.
Show you can read generator output as a diagnostic: a keyed table whose record sits on TableRecordImpl means the schema source did not report its key.
Weigh which outputs a codebase should allow: DAOs and POJOs add surface and coupling to tables, so a lead decides deliberately rather than enabling everything.
## What the code generator is **jOOQ's code generator** (the `jooq-codegen` module, run through `GenerationTool`, the Maven plugin `jooq-codegen-maven` or the Gradle plugin `org.jooq.jooq-codegen-gradle`) reads a schema from a **schema source**, builds an in-memory model of its tables, keys, sequences and routines, and writes Java or Kotlin source for that model. The default generator class is `org.jooq.codegen.DefaultGenerator`, which extends `JavaGenerator`; `org.jooq.codegen.KotlinGenerator` writes Kotlin instead. Output lands in the target package (default `org.jooq.generated`) under the target directory (default `target/generated-sources/jooq`, or `build/generated-sources/jooq` when the Gradle plugin fills it in). The generated classes are what make jOOQ queries compile against real tables: a renamed column becomes a compile error in every query that names it. ## What it writes for each table For a table `PAYMENT` in schema `PUBLIC`, the default naming strategy produces: - **The table class** `tables.Payment`: extends jOOQ's table implementation, holds a static singleton `PAYMENT`, and declares one `TableField<PaymentRecord, T>` per column, where `T` is the Java type the column maps to. - **The record class** `tables.records.PaymentRecord`: one instance holds one row. It extends `UpdatableRecordImpl` when the schema source reports a primary key and `TableRecordImpl` when it does not, so the record's abilities depend on what the source reported, not on what production holds. - **Optional extras per table**, only when enabled: a POJO `tables.pojos.Payment`, a DAO `tables.daos.PaymentDao` built on jOOQ's `DAOImpl`, and an interface `tables.interfaces.IPayment` that records and POJOs can share. ## What it writes once per schema Beside the per-table classes the generator writes holders that the rest of the code references: - `Tables`: a static reference to every table singleton, so `Tables.PAYMENT` works without importing each class. - `Keys`: primary, unique and foreign key constants, used by joins and by records that need their key. - `Indexes`: index constants, when `<indexes>` is on. - A **schema class** (for example `Public`) listing the schema's objects, and **`DefaultCatalog`** listing the schemas. - Sequence, routine and enum classes when the schema has them (a native enum type becomes a generated Java enum in an `enums` package). ## The `<generate>` flags and their defaults | Flag | Default | Writes | |---|---|---| | `tables` | `true` | table classes and `Tables` | | `records` | `true` | record classes | | `keys` | `true` | `Keys` | | `indexes` | `true` | `Indexes` | | `sequences`, `routines` | `true` | sequence and routine classes | | `relations` | `true` | key relationships between tables (forced on by tables or DAOs) | | `pojos` | `false` | plain value classes per table | | `immutablePojos` | `false` | POJOs with final fields | | `interfaces` | `false` | interfaces shared by records and POJOs | | `daos` | `false` | a DAO per table with a primary key | ## Flags that switch others on Some flags imply others, and the generator enforces the implication rather than failing: 1. `daos` implies **records** and **POJOs**: `AbstractGenerator.generateRecords()` returns true when DAOs are on, and so does `generatePojos()`. 2. `immutablePojos` implies **POJOs**. 3. **Relations** are forced on whenever tables or DAOs are generated: `generateRelations()` returns true if any of the three flags is set, so `<relations>false</relations>` alone changes nothing in a default run. 4. **Tables** are forced on whenever records or DAOs are generated, and when indexes are generated for a schema that has any, so `<tables>false</tables>` alone does not remove table classes. This is why a team that enables only `<daos>` still sees `pojos` and `records` packages appear. A DAO is skipped, with a log line, for any table that has no primary key. ## Java records and Kotlin data classes POJO style is configurable once `pojos` is on: - `pojosAsJavaRecordClasses` (default `false`) makes the Java generator write Java `record` types instead of mutable classes. - `pojosAsKotlinDataClasses` (default `true`) makes `KotlinGenerator` write `data class` POJOs. - `pojosEqualsAndHashCode` and `pojosToString` both default to `true`. ## What to say in an interview Name the two per-table outputs (table class and record class), the schema-wide holders, and the defaults: records on, POJOs and DAOs off. Mentioning that the record's base class depends on whether a primary key was found shows you know the generator is only as good as the schema it reads.
- Why might a generated jOOQ record extend TableRecordImpl although the table has a primary key in production?The default strategy picks `UpdatableRecordImpl` only when the schema source it read reports a primary key. If that source did not show the key, for example because the object is a view, or the database or scripts the generator read lack the constraint, the record is written on `TableRecordImpl` and loses key-based update and refresh. Fix the source, then regenerate.
- How do you get Java records or Kotlin data classes instead of mutable POJOs from jOOQ's generator?Enable `<pojos>` first. For the Java generator, set `<pojosAsJavaRecordClasses>true</pojosAsJavaRecordClasses>`. With `org.jooq.codegen.KotlinGenerator`, POJOs are already data classes, because `pojosAsKotlinDataClasses` defaults to true. For classic Java classes with final fields, `immutablePojos` is the switch, and it implies POJOs on its own.
- jOOQ generates POJOs and DAOs for every table out of the box.
- Every generated record is an UpdatableRecord, whether or not the table has a key.
- The generator writes only table classes; record classes are written by hand.
- Generated table classes expose column names as plain strings, not typed fields.