JOOQ – inline Converter not being applied

Tags: , , ,

In my build.gradle I use a converter in my forcedTypes. This works fine where i need it.

forcedType {
    userType = 'java.util.List<stormsensor.thor.dto.telemetry.FlowEventType>'
    converter = 'stormsensor.thor.config.jooq.StringToFlowEventTypeListConverter'
    includeExpression = '.*\.FLOW_EVENTS'
    includeTypes = '.*'

However, I am trying to convert a comma separated string into a list of enums for a specificuse case:

DataType<List<FlowEventType>> LIST_TYPE = SQLDataType.VARCHAR.asConvertedDataType(new StringToFlowEventTypeListConverter());
  groupConcatDistinct(NOTIFICATION_RULE.FLOW_EVENT_TYPE).as(field(name("notifications"), LIST_TYPE))

This throws an exception

org.jooq.exception.MappingException: An error ocurred when mapping record to class stormsensor.thor.dto.notification.batch.BatchNotificationRuleModel
    at org.jooq.impl.DefaultRecordMapper$
    at org.jooq.impl.ResultImpl.into(
    at org.jooq.impl.AbstractResultQuery.fetchInto(
    at org.jooq.impl.SelectImpl.fetchInto(
Caused by: org.jooq.exception.DataTypeException: Cannot convert from CRITICAL_DEPTH,NON_TIDAL_CSO (class java.lang.String) to interface java.util.List
    at org.jooq.impl.AbstractRecord.get(
    at org.jooq.impl.DefaultRecordMapper$
    at org.jooq.impl.DefaultRecordMapper$
    ... 72 more

This is the model I am fetching into:

public class BatchNotificationRuleModel implements Serializable {
    private static final long serialVersionUID = 1L;

    private List<FlowEventType> notifications;
    private List<MessageProtocolType> protocols;

Am I missing something?


I am able to convert inline using

.fetchStream().map(e -> {
    Converter<String, List<FlowEventType>> converter = new StringToFlowEventTypeListConverter();
    List<FlowEventType> notifications = e.get(field(name("notifications"), String.class), converter);
    return BatchNotificationRuleModel.builder().notifications(notifications).build();

What is the difference between the initial converter I apply vs the late stage map conversion?


While it seems reasonable to expect that would use the argument field’s name and type for type coercion, this is not the case. As per the Javadoc:

Create an alias for this field based on another field’s name.

In order to coerce your expression to the desired data type, you have to do this, manually:


See Field.coerce(DataType)

Source: stackoverflow