2

I have jooq create TableImpl POJO name CONNECTOR with variable joda.time.DateTime

and jsonschema2pojo create POJO name JsonConnector with variable java.time.Instant

how to convert jooq select result fetch to jsonschema2pojo create obj

List <JsonConnector> list = ctx.select(
    CONNECTOR.INT1,
    CONNECTOR.STRING1,
    CONNECTOR.STRING2,
    CONNECTOR.STRING3,
    CONNECTOR.STRING4,
    CONNECTOR.JODA_TIME_DATETIME))
    .from(CONNECTOR)
    .fetchInto(JsonConnector.class);

jooq fetchInto jsonschema2pojo everything simple type convert is ok, but pojo type org.joda.time.DateTime to type java.time.Instant

that will throw exception org.jooq.exception.MappingException: An error ocurred when mapping record to class JsonConnector

how can I write function like TableField<ConnectorRecord, Instant> convertDateTimeToInstant(TableField<ConnectorRecord, DateTime> datatimeField) or something good method

Thanks

deniro.wang
  • 53
  • 1
  • 5
  • What's the reason why you're using the Joda time library, when at the same time you're also using JSR 310 types? – Lukas Eder Apr 27 '23 at 13:34
  • I took over and maintained a project for a while. It ran very well with jOOQ and DateTime, and we had lots of functions to manipulate DateTime and strings. We received a new request regarding a JSON issue, so I used jsonschema2pojo. When you asked me, I suddenly realized that I had misunderstood and thought jOOQ could only work with DateTime. I changed "org.joda.time.LocalDate" to "java.time.Instant" and it ran smoothly. – deniro.wang Apr 27 '23 at 15:32
  • but, if I change jooq forcedType from DateTime to Instant, our original code must modify lots. so back to my question, can we have other solution? – deniro.wang Apr 27 '23 at 15:39

1 Answers1

1

Register a global ConverterProvider

For the particular use-case you have here, you can implement a ConverterProvider that is able to convert between these two data types. ConverterProvider is an SPI that allows to override the default data type conversions between two data types, such as DateTime and Instant, in both directions.

Roughly:

class MyConverterProvider implements ConverterProvider {
    final ConverterProvider delegate = new DefaultConverterProvider();
    
    @Override
    public <T, U> Converter<T, U> provide(Class<T> tType, Class<U> uType) {
        if (tType == DateTime.class && uType == Instant.class) {
            return Converter.ofNullable(tType, uType,
                t -> (U) dateTimeToInstant((DateTime) t),
                u -> (T) instantToDateTime((Instant) u)
            );
        }
        
        // Delegate all other type pairs to jOOQ's default
        else
            return delegate.provide(tType, uType);
    }
}

You then set that to your local or global Configuration:

configuration.set(new MyConverterProvider());

Using a local ad-hoc converter

You can always attach a local ad-hoc converter to a field, effectively chaining converters

List <JsonConnector> list = ctx.select(
    CONNECTOR.INT1,
    CONNECTOR.STRING1,
    CONNECTOR.STRING2,
    CONNECTOR.STRING3,
    CONNECTOR.STRING4,
    CONNECTOR.JODA_TIME_DATETIME.convertFrom(d -> dateTimeToInstant(d)))
    .from(CONNECTOR)
    .fetchInto(JsonConnector.class);
Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509