How to set fields non-nullable for JOOQ POJO

Viewed 20

I have this DDL

CREATE TABLE user
(
    id       bigint NOT NULL PRIMARY KEY,
    name string NOT NULL
);

org.jooq.codegen.KotlinGenerator generates such a class:

data class Users(
    var id: Long? = null,
    var name: String? = null,
): Serializable

I expect non-nullable fields to be non-nullable, like:

data class Users(
    var id: Long,
    var name: String,
): Serializable

I only use these settings:

    generate.apply {
                isRecords = true
                isDaos = true
                isPojosAsKotlinDataClasses = true
                isSpringAnnotations = true
                isPojos = true
            }

Jooq ver. 3.15.10

How do I configure the generator so that the fields are non-nullable?

1 Answers

As of jOOQ 3.17, there are pending feature requests to add some additional convenience at the price of correctness to generated classes:

  • #10212 Add option to handle nullability in KotlinGenerator
  • #12934 Null-safe views of columns known to be non-null

The main reason why this hasn't been implemented yet is the many ways such non-nullability promises can break in the event of using operators like LEFT JOIN, UNION and many others, in case of which an expression that appears to be non-null will produce null values nonetheless.

While #10212 is very hard to get right, #12934 might be a compromise to be implemented soon, given that it affects only generated data class types, which can be used on a 1:1 basis by users who understand the tradeoffs.

Related