skip to content

In jOOQ, when does a custom column type need a Binding rather than just a Converter, and what breaks if you choose wrongly?

level: seniorimportance: should knowfreq 32%

answer

  1. value mapping versus JDBC plumbing
  2. Converter: from() reads, to() writes
  3. Binding: sql, set, get, register
  4. a cast such as ?::json
  5. nested MULTISET and ROW values

basics

~20 s

A Converter<T, U> maps a type JDBC already handles to your own type and back; a Binding<T, U> also controls the JDBC plumbing: bind-marker SQL, PreparedStatement set and result get. Use a Binding only when the driver or SQL needs it.

solid answer

~50 s

`Converter<T, U>` has `from(T)` for reading and `to(U)` for writing, where `T` is a type jOOQ and the driver already handle: a `Long` column read as a `CustomerId`, or a `String` read as an enum. It runs when results are read and values are bound, recursively inside `multiset(...)` and `row(...)` nesting, and never changes the SQL. `Binding<T, U>` carries a converter plus the low-level hooks: `sql(...)` renders the bind marker, such as `?::json` on PostgreSQL, `set(...)` writes to the `PreparedStatement`, `get(...)` reads from the `ResultSet`, `CallableStatement` or `SQLInput`, and `register(...)` declares `OUT` parameters. You need one when the driver cannot carry the value or the SQL needs a cast. Choosing wrongly shows up two ways: a Converter alone where the database rejects the bound type, or conversion logic placed only in a Binding's `get(...)`, which never runs for values nested in `multiset` or `row`, because a Binding applies only to JDBC interactions.

code

java · 13 lines
java
import org.jooq.Converter;
import org.jooq.DataType;
import org.jooq.impl.SQLDataType;

public record CustomerId(long value) {}

Converter<Long, CustomerId> customerIdConverter = Converter.ofNullable(
    Long.class, CustomerId.class,
    CustomerId::new,      // from: reading BIGINT into CustomerId
    CustomerId::value);   // to: writing CustomerId as BIGINT

DataType<CustomerId> customerIdType =
    SQLDataType.BIGINT.asConvertedDataType(customerIdConverter);

go deeper

for a junior

Recall that a Converter maps a database value to your type and back, and that from() means reading while to() means writing.

for a middle

Explain where each runs: a Converter on values, recursively in nested results; a Binding on the JDBC calls, with sql, set, get and register.

for a senior

Diagnose the wrong choice: a database rejecting a bound string for a json column, or a conversion that works top-level but not inside MULTISET.

for a principal

Set a rule that custom types start as Converters on jOOQ's built-in types, with a Binding only after the driver or SQL proves it necessary.

## Two extension points, two layers Why a data layer converts between column values and domain types is a general mapping topic. jOOQ gives you two interfaces for it, and they sit at different layers: | | `Converter<T, U>` | `Binding<T, U>` | |---|---|---| | Layer | values, after JDBC has read or before it writes | the JDBC interaction itself | | Methods | `from(T)`, `to(U)`, `fromType()`, `toType()` | `converter()`, `sql(...)`, `register(...)`, `set(...)`, `get(...)` | | Changes the SQL? | never | can, through `sql(...)` | | Applies to `multiset` / `row` contents | yes, recursively | no, only JDBC interactions | | Typical use | wrapped ids, enums, money, time types | a vendor type the driver cannot bind plainly | `T` is always a **database type** jOOQ knows, from `SQLDataType`; `U` is your **user type**. "From" means reading from the database, "to" means writing to it. ## Converter in practice - Build one with `Converter.of(...)` or `Converter.ofNullable(...)`, which passes `null` through without calling your functions. jOOQ also ships `EnumConverter` for enum columns. - jOOQ recommends, without requiring, that `from` and `to` be **reciprocal**: converting there and back yields the original value. It also expects every implementation to cope with `null`. - Attach it to a type with `SQLDataType.BIGINT.asConvertedDataType(converter)`, to a generated column through the code generator's forced types, or to one expression with an **ad-hoc converter**: `CUSTOMER.ID.convertFrom(CustomerId::new)`. An ad-hoc converter is one-way and applies only when the result is read; it has no effect on the SQL. - Because it is applied recursively, a converter on a field also converts that field's values inside `multiset(...)` and `row(...)` results. ## When a Binding is required A Binding is the escape hatch for what a Converter cannot reach: - **The statement needs special bind syntax**, for example a cast. The `Binding` documentation's own example is rendering `?::json` for PostgreSQL's JSON type, so the database accepts a string parameter as `json`. - **The driver needs a specific call**: `set(...)` decides how the value goes onto the `PreparedStatement` (`setObject` with a vendor type, for instance), and `get(...)` how it comes back from a `ResultSet`. - **Stored procedures and UDTs**: `register(...)` declares `OUT` parameters on a `CallableStatement`, and the `SQLInput` / `SQLOutput` variants handle user-defined type attributes. - **Consistency**: the source requires `sql(...)` and `set(...)` to agree. If a value is inlined into the SQL, it must not also be bound. Before writing one, check whether jOOQ already supports the type. jOOQ ships `JSON` and `JSONB` types (`SQLDataType.JSONB`), so a JSON document column usually needs only a `Converter` from `JSONB` to your class. ## What breaks when you pick wrongly 1. **Converter where a Binding was needed**: jOOQ binds the database-side `T`, for example a `String`, and the database rejects it, as PostgreSQL does with "column is of type json but expression is of type character varying". 2. **Binding where a Converter would do**: more code to own across reads, writes, inlined rendering and `OUT` parameters, and more ways for `sql(...)` and `set(...)` to disagree. 3. **Logic only in `Binding.get(...)`**: top-level columns convert correctly, but the same column inside a `multiset(...)` or `row(...)` can come back unconverted, because jOOQ documents that a Binding applies only to JDBC interactions and does not influence how `multiset` or `row` contents are mapped to Java objects. 4. **A non-reciprocal Converter**: a value read and later written back, or used as a bind value in a `WHERE` clause, reaches the database in a different form than it was stored, so updates drift and filters miss rows. ## A diagnosis path Most custom types in a jOOQ codebase, ids, enums, amounts, time types and JSON documents mapped from `JSONB`, need nothing beyond a Converter. A Binding is worth its extra code only for genuinely vendor-specific types or procedure calls. When a custom type misbehaves, ask in order: does the driver accept the plain database type (if yes, a Converter suffices); does the SQL need a cast or vendor syntax (if yes, a Binding); does the value also appear in nested collections (if yes, make sure the conversion lives in a Converter, not only in a Binding's `get(...)`); and does the round trip return the original value.

  • Why does an ad-hoc convertFrom on a selected jOOQ field not change the SQL that is sent?
    `convertFrom` attaches a one-way `Converter` that is applied only when the `Result` is read. The rendered SQL and what the database returns are unchanged, exactly as with a converter attached by the code generator. If the database itself must produce a different value or type, you need a SQL expression, such as a cast, not a converter.
  • What goes wrong if a jOOQ Converter's from() and to() are not reciprocal?
    jOOQ recommends, but does not enforce, that converting to the user type and back returns the original value. When that fails, a value read and written back, or used as a bind value in a `WHERE` clause, reaches the database in a different form, for example trimmed, rounded or re-cased. Updates then drift and equality filters miss rows.

  • Every custom Java type in jOOQ needs its own Binding
  • A jOOQ Converter changes the SQL rendered for the column
  • A Binding's get() also converts values nested inside MULTISET
  • Converter.from() is the writing direction, toward the database
  • A Converter can ignore null because jOOQ never passes it one