JOOQ - 未应用内联转换器
JOOQ - inline Converter not being applied
在我的 build.gradle
中,我在 forcedTypes
中使用了转换器。这在我需要的地方工作得很好。
forcedType {
userType = 'java.util.List<stormsensor.thor.dto.telemetry.FlowEventType>'
converter = 'stormsensor.thor.config.jooq.StringToFlowEventTypeListConverter'
includeExpression = '.*\.FLOW_EVENTS'
includeTypes = '.*'
}
但是,我正在尝试将逗号分隔的字符串转换为特定用例的枚举列表:
DataType<List<FlowEventType>> LIST_TYPE = SQLDataType.VARCHAR.asConvertedDataType(new StringToFlowEventTypeListConverter());
ctx.select(
groupConcatDistinct(NOTIFICATION_RULE.FLOW_EVENT_TYPE).as(field(name("notifications"), LIST_TYPE))
)
.from(NOTIFICATION_RULE)
.groupBy(MONITORING_POINT_ID)
.fetchInto(BatchNotificationRuleModel.class);
这会引发异常
org.jooq.exception.MappingException: An error ocurred when mapping record to class stormsensor.thor.dto.notification.batch.BatchNotificationRuleModel
at org.jooq.impl.DefaultRecordMapper$MutablePOJOMapper.map(DefaultRecordMapper.java:802)
at org.jooq.impl.DefaultRecordMapper.map(DefaultRecordMapper.java:500)
at org.jooq.impl.ResultImpl.into(ResultImpl.java:1284)
at org.jooq.impl.AbstractResultQuery.fetchInto(AbstractResultQuery.java:1550)
at org.jooq.impl.SelectImpl.fetchInto(SelectImpl.java:3746)
at stormsensor.thor.repository.notification.BatchNotificationRuleRepositoryJdbcImpl.save(BatchNotificationRuleRepositoryJdbcImpl.java:98)
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.tools.Convert$ConvertAll.fail(Convert.java:1194)
at org.jooq.tools.Convert$ConvertAll.from(Convert.java:1083)
at org.jooq.tools.Convert.convert0(Convert.java:324)
at org.jooq.tools.Convert.convert(Convert.java:316)
at org.jooq.tools.Convert.convert(Convert.java:387)
at org.jooq.impl.AbstractRecord.get(AbstractRecord.java:275)
at org.jooq.impl.DefaultRecordMapper$MutablePOJOMapper.map(DefaultRecordMapper.java:830)
at org.jooq.impl.DefaultRecordMapper$MutablePOJOMapper.map(DefaultRecordMapper.java:762)
... 72 more
这是我要提取的模型:
@Data
@NoArgsConstructor
public class BatchNotificationRuleModel implements Serializable {
private static final long serialVersionUID = 1L;
private List<FlowEventType> notifications;
private List<MessageProtocolType> protocols;
}
我是不是漏掉了什么?
更新:
我可以使用
进行内联转换
//...
.groupBy(MONITORING_POINT_ID)
.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();
}).collect(toList());
我应用的初始转换器与后期地图转换有什么区别?
虽然预期 Field.as(Field)
会使用参数字段的名称 和 类型进行类型强制转换似乎是合理的,但事实并非如此。根据 Javadoc:
Create an alias for this field based on another field's name.
为了将表达式强制转换为所需的数据类型,您必须手动执行此操作:
groupConcatDistinct(NOTIFICATION_RULE.FLOW_EVENT_TYPE)
.coerce(LIST_TYPE)
.as("notifications")
在我的 build.gradle
中,我在 forcedTypes
中使用了转换器。这在我需要的地方工作得很好。
forcedType {
userType = 'java.util.List<stormsensor.thor.dto.telemetry.FlowEventType>'
converter = 'stormsensor.thor.config.jooq.StringToFlowEventTypeListConverter'
includeExpression = '.*\.FLOW_EVENTS'
includeTypes = '.*'
}
但是,我正在尝试将逗号分隔的字符串转换为特定用例的枚举列表:
DataType<List<FlowEventType>> LIST_TYPE = SQLDataType.VARCHAR.asConvertedDataType(new StringToFlowEventTypeListConverter());
ctx.select(
groupConcatDistinct(NOTIFICATION_RULE.FLOW_EVENT_TYPE).as(field(name("notifications"), LIST_TYPE))
)
.from(NOTIFICATION_RULE)
.groupBy(MONITORING_POINT_ID)
.fetchInto(BatchNotificationRuleModel.class);
这会引发异常
org.jooq.exception.MappingException: An error ocurred when mapping record to class stormsensor.thor.dto.notification.batch.BatchNotificationRuleModel
at org.jooq.impl.DefaultRecordMapper$MutablePOJOMapper.map(DefaultRecordMapper.java:802)
at org.jooq.impl.DefaultRecordMapper.map(DefaultRecordMapper.java:500)
at org.jooq.impl.ResultImpl.into(ResultImpl.java:1284)
at org.jooq.impl.AbstractResultQuery.fetchInto(AbstractResultQuery.java:1550)
at org.jooq.impl.SelectImpl.fetchInto(SelectImpl.java:3746)
at stormsensor.thor.repository.notification.BatchNotificationRuleRepositoryJdbcImpl.save(BatchNotificationRuleRepositoryJdbcImpl.java:98)
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.tools.Convert$ConvertAll.fail(Convert.java:1194)
at org.jooq.tools.Convert$ConvertAll.from(Convert.java:1083)
at org.jooq.tools.Convert.convert0(Convert.java:324)
at org.jooq.tools.Convert.convert(Convert.java:316)
at org.jooq.tools.Convert.convert(Convert.java:387)
at org.jooq.impl.AbstractRecord.get(AbstractRecord.java:275)
at org.jooq.impl.DefaultRecordMapper$MutablePOJOMapper.map(DefaultRecordMapper.java:830)
at org.jooq.impl.DefaultRecordMapper$MutablePOJOMapper.map(DefaultRecordMapper.java:762)
... 72 more
这是我要提取的模型:
@Data
@NoArgsConstructor
public class BatchNotificationRuleModel implements Serializable {
private static final long serialVersionUID = 1L;
private List<FlowEventType> notifications;
private List<MessageProtocolType> protocols;
}
我是不是漏掉了什么?
更新:
我可以使用
进行内联转换//...
.groupBy(MONITORING_POINT_ID)
.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();
}).collect(toList());
我应用的初始转换器与后期地图转换有什么区别?
虽然预期 Field.as(Field)
会使用参数字段的名称 和 类型进行类型强制转换似乎是合理的,但事实并非如此。根据 Javadoc:
Create an alias for this field based on another field's name.
为了将表达式强制转换为所需的数据类型,您必须手动执行此操作:
groupConcatDistinct(NOTIFICATION_RULE.FLOW_EVENT_TYPE)
.coerce(LIST_TYPE)
.as("notifications")