We use jOOQ 3.15.4 with Spring Boot 2.6.2
We have an enum stored as a string in database. It looks like this in the generated code:
public final TableField<StatusRecord, String> STATUS = createField(DSL.name("status"), SQLDataType.VARCHAR(255).nullable(false), this, "");
When reading it, we fetch into an object with @Column
annotations:
@Column(name = "status")
StatusEnum status
The behavior we see is that when we have a string in DB that does not correspond to any value of the enum, the status
field in the object that we map into is null.
Is there a way to have this type safe, and have a MappingException
thrown when the string cannot be mapped to the enum, like the standard behavior of the valueOf
method of an enum ?
The topic has been discussed a few times in jOOQ's issue tracker, mailing list, etc. There's a pending feature request to change the current behaviour of ignoring inconvertible values to throwing exceptions, see e.g. https://github.com/jOOQ/jOOQ/issues/3377
It wasn't easy to do in the past without breaking compatibility as the conversions were implemented in static methods. But since jOOQ 3.14, we have a ConverterProvider
SPI, so it might be possible to make this configurable, and gradually move towards a fail-fast strategy.
Having said so, you can implement your own ConverterProvider
and add such checks to it. Alternatively, you can attach a custom Converter
to your Field
using code generation configuration, and let it throw whenever an invalid value is mapped.