kotlinjooq

Jooq setNull sets the column to 0 instead of NULL


I'm using Jooq against an Oracle DB with the Jooq CodeGen and Kotlin Generator. The following code should be setting the GROUP_ID to NULL, but instead it is setting the GROUP_ID = 0.

fun removeUserGroup(userId: Long): Int {
    return dsl
        .update(USER)
        .setNull(USER.GROUP_ID)
        .where(USER.ID.eq(userId))
        .execute()

Gradle config for forced types

forcedType {
  userType = "java.lang.Long"
  includeTypes = "NUMBER"
}

I'm guessing Jooq is treating these are primitive Longs in the conversion? The generated tables/columns are correctly Long?. I'm not certain how to fix this.

Or is this usecase to be avoided and I should not use the forcedType and stick with the generated BigDecimal?


Solution

  • It appears there's a bug in jOOQ 3.20.8 and earlier, where the KotlinGenerator produces a Long type (i.e. a Java long primitive type that cannot represent null as a value), instead of a Long? type, when using this auto conversion configuration:

    The generated code looks similar to this:

    AutoConverter<BigInteger, Long>(BigInteger::class.java, Long::class.java)
    

    Only the KotlinGenerator is affected, not the JavaGenerator or ScalaGenerator.

    The bug is:

    Using type rewriting as a workaround should help here:

    forcedType {
      name = "BIGINT"
      includeTypes = "NUMBER"
    }
    

    Alternatively, you don't have to rely on jOOQ's AutoConverter feature. You can provide an explicit converter yourself:

    forcedType {
      userType = "java.lang.Long"
      converter = ...
      includeTypes = "NUMBER"
    }