As the lead of a jOOQ codebase, would you allow active-record writes through UpdatableRecord.store(), or require explicit INSERT and UPDATE statements?
answer
- implicit statement choice versus visible SQL
- partial updates from touched flags
- records built from DTOs insert
- optimistic locking is opt-in
- a rule per write shape
basics
~20 sNeither extreme suits every team: store() gives concise partial updates and opt-in optimistic locking, but hides the INSERT-or-UPDATE choice in key flags. A common rule allows it for fetch-edit-store of one row and requires explicit statements elsewhere.
solid answer
~50 sI would decide per write shape rather than ban or bless `store()`. For a back-office edit, fetch a `CustomerRecord`, change two fields, `store()`, it is hard to beat: it writes only touched columns, reads back identity values, and with `Settings.executeWithOptimisticLocking` plus generated version fields it rejects stale edits with `DataChangedException`. Its costs are real: the `INSERT`-or-`UPDATE` choice depends on key flags, so a record built from a DTO with `newRecord(table, form)` inserts a duplicate; locking is off by default; and the SQL is invisible in review. So my rule would be: `store()` only on records fetched in the same unit of work, with optimistic locking enabled in the shared `Configuration`; explicit `insertInto`, `update` or `onConflict().doUpdate()` statements for DTO-driven updates, bulk changes, upserts and multi-table writes; and an architecture test that keeps record writes in the persistence package.
go deeper
Recall the two write styles in jOOQ: store() on generated records, and explicit insertInto or update statements built with the DSL.
Explain what store() decides for you: the statement type from key flags and the columns from touched flags.
Show the failures you have seen: duplicate inserts from DTO-built records, whole-row writes, and lost updates with locking left off.
Own the rule: which write shapes may use store(), which settings are fixed team-wide, and how the rule is enforced in tests and review.
## The decision jOOQ supports two write styles. **Active-record writes** use generated `UpdatableRecord`s: fetch or create a record, set fields, call `store()`, `update()`, `delete()` or `batchStore(...)`. **Explicit statements** use the DSL: `dsl.insertInto(CUSTOMER)...`, `dsl.update(CUSTOMER).set(...).where(...)`, `onConflict(...).doUpdate()`. Both are idiomatic jOOQ. A lead's job is to decide where each is allowed, because mixing them without a rule produces bugs that only appear under real data. ## What store() gives a team - **Partial updates for free**: only touched fields are written, so an edit form that changes one column sends a one-column `UPDATE`, and an insert leaves unset columns to their database defaults. - **Generated values back in the object**: identity values are read back after an insert (`Settings.returnIdentityOnUpdatableRecord`, default `true`). - **Opt-in optimistic locking**: with `Settings.executeWithOptimisticLocking` and version or timestamp fields declared to the code generator, `store()` and `delete()` add the version check and throw `DataChangedException` on a conflict. - **Little code** for screens that load one row, edit it and save it, which describes much of a back-office. ## What it costs - **An implicit statement choice**: `store()` picks `INSERT` or `UPDATE` from the key's touched flags, never from the database. A record filled from request data, key included, inserts. - **Locking off by default**: `executeWithOptimisticLocking` defaults to `false`, so without a team-wide setting, concurrent edits overwrite each other's touched columns. - **Whole-row writes by accident**: `newRecord(table, pojo)` marks every field touched, so storing it writes every column, including ones the user never saw. - **Hidden SQL**: reviewers see `customer.store()`, not the statement, and cannot spot a missing condition or an unintended insert. - **No set-based change**: `store()` and `batchStore` act row by row on records in memory, and `store()` is not an upsert (the record's upsert is `merge()`). ## Comparing the two styles | Concern | `UpdatableRecord.store()` | Explicit DSL statements | |---|---|---| | Statement visible in code | no | yes | | Partial update | automatic, from touched flags | you list the columns | | Optimistic locking | built in, once enabled | you add the version condition | | Upsert | `merge()`, not `store()` | `onConflict(...).doUpdate()` | | Set-based change | not supported | `update(...).where(...)` on many rows | | Risk from DTO input | duplicate insert, whole-row write | only the columns you set | ## Rules teams adopt 1. **Records everywhere**: fast to write, acceptable for small internal tools with one writer, risky as the team grows. 2. **Explicit statements only**: every write visible and reviewable, at the cost of more code and hand-written version checks. 3. **Split by write shape**, the usual landing point: `store()` for fetch-edit-store of one aggregate inside one unit of work; explicit statements for DTO-driven updates, upserts, bulk changes, imports and writes spanning tables. A back-office with a handful of developers editing customer rows through forms fits option 3 well: most writes are single-row edits, and the few bulk jobs, such as a nightly price update or a supplier import, are exactly where explicit statements or the Loader belong. Whichever rule you pick, write down the reason. The constraint that decides it is usually the shape of the writes and how many people review the persistence code, not taste. ## Making the rule stick - Set the write-related `Settings` once, in the shared `Configuration`: `executeWithOptimisticLocking`, and leave `updatablePrimaryKeys` at `false` so a changed key never rewrites a row. - Declare version or timestamp columns to the code generator so locking uses a `WHERE` condition rather than a `SELECT … FOR UPDATE` re-read. - Keep record writes in a persistence package and add an architecture test that flags `store()`, `batchStore(...)` and `newRecord(table, source)` outside it. - Add a review checklist line: for every `store()`, where was the record fetched, and is locking on? - Use `dsl.executeUpdate(record)` when an explicit update of a record built from input is intended; it updates by key and makes the intent visible. The principal-level point is that the choice is not about style. It is about where the INSERT-or-UPDATE decision is made, and who can see it.
- If the team allows store(), which jOOQ settings would you standardise in the shared Configuration?`executeWithOptimisticLocking` set to `true`, with version or timestamp columns declared to the code generator so the check is a `WHERE` condition rather than a `SELECT … FOR UPDATE` re-read; `updatablePrimaryKeys` left `false` so a changed key never rewrites a row; and `recordDirtyTracking` left at `TOUCHED` unless the team deliberately wants only real value changes written.
- How would you enforce that bulk changes and upserts use explicit jOOQ statements?Make the rule checkable rather than tribal: keep writes in a persistence package, add an architecture test that rejects `store()`, `batchStore(...)` and `newRecord(table, source)` outside allowed classes, and add a review checklist item. Back it with integration tests that assert the rows written, since `store()` hides the statement it chose.
- UpdatableRecord.store() is always unsafe and should be banned outright
- store() protects against lost updates out of the box
- store() upserts, so a DTO carrying an ID safely updates the existing row
- Explicit DSL updates cannot do partial updates, so they overwrite every column
- batchStore turns any store() loop into one efficient batch automatically