How to Write a Quick and Dirty Converter in jOOQ

One of jOOQ‘s most powerful features is the capability of introducing custom data types, pretending the database actually understands them. For instance, when working with SQL TIMESTAMP types, users mostly want to use the new JSR-310 LocalDateTime, rather than the JDBC java.sql.Timestamp type. In jOOQ 3.9+, this is a no brainer, as we’ve finally introduced the <javaTimeTypes> flag to automatically generate JSR 310 types instead of JDBC types. But sometimes, you want some custom conversion behaviour, so you write a Converter. To the rescue our new jOOQ 3.9+ converter constructors, which essentially take two lambdas to construct a converter for you. For instance:

Converter<Timestamp, LocalDateTime> converter =
    t -> t == null ? null : t.toLocalDateTime(),
    u -> u == null ? null : Timestamp.valueOf(u)

And you’re set! Even easier, if you don’t need any special null encoding (as above), just write this equivalent converter, instead:

Converter<Timestamp, LocalDateTime> converter =

Where’s that useful? The code generator needs a concrete converter class, so you cannot use that with the code generator, but there are many other places in the jOOQ API where converters are useful, including when you write plain SQL like this:


2 thoughts on “How to Write a Quick and Dirty Converter in jOOQ

  1. Would be nice to see how these could be used to covert result of an expression from SELECT clause. Is there something like this?, "notnull", "null").convert(myConverter)).from(...)...

    1. Thanks for your message. You always have to work with a DataType. In your case, I would assume this could work:

      DataType myType = SQLDataType.VARCHAR.asConvertedDataType(myConverter);
      Field myField = nvl2(MY_TABLE.MY_COLUMN, "notnull", "null").coerce(myType);

      And now use that field in SQL

Leave a Reply