Does jOOQ provide support for JSR310 in combination with PostgreSQL? In particular, I am trying to use the following classes:
java.time.Instant
java.time.LocalDate
java.time.LocalTime
java.time.LocalDateTime
I am storing in the following data types (as per http://www.postgresql.org/docs/9.1/static/datatype-datetime.html):
java.time.Instant
:timestamp with timezone
java.time.LocalDate
:date
java.time.LocalTime
:time without timezone
java.time.LocalDateTime
:timestamp without timezone
Are these data types correct?
Does jOOQ support translation between java.sql.Timestamp
, java.sql.Date
and java.sql.Time
and the four classes above (in both directions)?
JSR-310 support is now available in jOOQ 3.9.0 to configure in code:
The same structure would apply in the xml config (Configuration - Generator - Generate - javaTimeTypes)
Keep an eye on https://github.com/jOOQ/jOOQ/issues/5714 to see when this is enabled by default.
jOOQ Roadmap
Up until jOOQ 3.6, there was no official support for JSR-310 date time types, because both the jOOQ Open Source Edition and the commercial editions still supported Java 6.
With jOOQ 3.7, this changes as Java 6 is supported only in a specifically built commercial distribution, whereas standard distributions will require Java 8. The relevant issue for this change is #4338.
Another change involves a flag
<javaTimeTypes/>
to tell the source code generator to generate JSR-310 types rather thanjava.sql.Date
and similar types. By default, this flag is set to false, so you will need to set it to true until issue #5714 is resolved.Using JSR-310 types with jOOQ 3.6 or less
In the meantime, you can bind the desired types yourself and let the code generator use your custom
Converter
orBinding
. See the relevant sections in the manual:The correct mapping
The JDBC 4.2 spec defines which JSR-310 data types should be mapped to which JDBC data types (which coincide with PostgreSQL's understanding of the same). In particular, the JDBC spec lists:
java.time.LocalDate
to JDBCDATE
.java.time.LocalTime
to JDBCTIME
java.time.LocalDateTime
to JDBCTIMESTAMP
.java.time.OffsetTime
to JDBCTIME_WITH_TIMEZONE
.java.time.OffsetDateTime
to JDBCTIMESTAMP_WITH_TIMEZONE
.JDBC's understanding does not seem entirely correct, though, as few databases really store the timezone information in a
TIMESTAMP WITH TIME ZONE
data type (e.g. Oracle does), see also this DBA/StackExchange answer: https://dba.stackexchange.com/a/59021/2512. Since the time zone is not stored, your approach of mappingjava.time.Instant
toTIMESTAMP WITH TIME ZONE
is better, because the SQL type is really just a UTC timestamp.Having said so,
OffsetDateTime
can still be mapped toTIMESTAMP WITH TIME ZONE
, but you will not know what time zone was used when the timestamp was stored in PostgreSQL.