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
jOOQ can only auto-map EnumType
enums to a PostgreSQL enum
type out of the box, not custom enum types. The main reasons include:
cast(? as users.some_type)
or when using arrays: cast(? as users.some_type[])
. It just wouldn't work, otherwise, with pgjdbc.EnumType.getLiteral()
for each value, even if in many cases, that's the same value as the Java/kotlin literal for the enum.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.
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.