jOOQ自定义绑定:转换器实现
背景
I have a Trino database with a column of type array(row(id varchar, version varchar))
The column values are stores like this:
['{id=223, version=3.0}','{id=132, version=3.0}','{id=163, version=3.0}','{id=210, version=3.0}','{id=150, version=3.0}','{id=1KXCLD, version=3.0}','{id=186, version=3.0}']
I convert the values of this columns using a Java record
public record MapperRecord(String id, String version)
The signature of the org.jooq.Binder implementation is
public class MyBinder implements Binding<Object[], Set<MapperRecord>> {
问题
I implement a org.jooq.Binding that works fine for insert and select, but the org.jooq.impl.Converter fails when executing a query with in clause
jooqDslTrinoContext.selectFrom(TABLE)
.where(TABLE.COLUMN.`in`(setOf(MapperRecord("a", "b"))))
.fetch()
.intoMaps()
The error I get is
org.jooq.exception.DataTypeException: Cannot convert from MapperRecord[id=a, version=b] (class MapperRecord) to class [Ljava.lang.Object;
at org.jooq.impl.Convert$ConvertAll.fail(Convert.java:1804)
This query works:
jooqDslTrinoContext.selectFrom(TABLE)
.fetch()
.intoMaps()
The problem seems to be that there's no strategy matching org.jooq.impl.Convert.U from(Object from, ConverterContext scope) implementation
My org.jooq.Converter implementation is:
new Converter<>() {
@Override
public Set<MapperRecord> from(Object[] databaseObject) {
...
}
@Override
public Object[] to(Set<MapperRecord> userObject) {
....
}
@Override
public Class<Object[]> fromType() {
return Object[].class;
}
@Override
@SuppressWarnings("unchecked")
public Class<Set<MapperRecord>> toType() {
return (Class<Set<MapperRecord>>) (Class<?>) Set.class;
}
};
Only fromType and toType are called
解决方案
As per the Binding::converter Javadoc:
虽然绑定的Converter属性不是可选的(
Converter.fromType()和Converter.toType()由jOOQ的内部机制所需要),但是如果实现不依赖它,转换实现(即Converter.from(Object)和Converter.to(Object))并非严格必需。
所以,当你不在绑定内部使用转换器时,转换实现就没有相关性。