javapostgresqlkotlinjooqjooq-codegen

Why doesn't Jooq recognise the type?


I have an enum SomeType with following structure:

enum class SomeType {
    Type1, 
    Type2
}

I also have a table with that enum:

create type users.some_type as enum ('Type1','Type2');
create table users.some
(
    id   bigint default nextval('users.some_seq'),
    type users.some_type not null,
    primary key (id)
);

The enum is generated as:

@Suppress("UNCHECKED_CAST")
enum class SomeType(@get:JvmName("literal") public val literal: String) : EnumType {
    Type1("Type1"),
    Type2("Type2");
    override fun getCatalog(): Catalog? = schema.catalog
    override fun getSchema(): Schema = Users.USERS
    override fun getName(): String = "some_type"
    override fun getLiteral(): String = literal
}

However, when I try to write an insert method for that table, it doesn't recognise the enum type:

@Transactional
    fun save(some: Some) =
        dslContext.insertInto(
            SOME,SOME.TYPE,
        ).values(some.someType)

I'm using Kotlin and Postgres, however I don't think that this problem is Kotlin-specific


Solution

  • Why doesn't it work

    jOOQ can only auto-map EnumType enums to a PostgreSQL enum type out of the box, not custom enum types. The main reasons include:

    As such, you cannot expect your own user defined type to "just work". The fact that it is an enum doesn't change that. It's no different from e.g. a BIGINT column in PostgreSQL being mapped to something like MyType. jOOQ can't just assume any mapping.

    How to get it to work

    You can easily attach an enumConverter (or any other type of converter) to your generated column. Behind the scenes, jOOQ will still use the generated converter for variable binding, but at least in your user code, you'll only see your own type now.