jOOQ - 支持JSR310 [英] jOOQ - support for JSR310
问题描述
jOOQ是否支持JSR310与PostgreSQL结合使用?特别是,我正在尝试使用以下类:
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
java.time.Instant
java.time.LocalDate
java.time.LocalTime
java.time.LocalDateTime
我存储在以下数据类型中(根据 http://www.postgresql.org/docs/9.1/static/datatype-datetime.html ):
I am storing in the following data types (as per http://www.postgresql.org/docs/9.1/static/datatype-datetime.html):
-
java.time.Instant
:带时区的时间戳
-
java.time.LocalDate
:date
-
java.time。 LocalTime
:没有时区的时间
-
java.time.LocalDateTime
:没有时区的时间戳
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?
jOOQ是否支持 java.sql.Timestamp
, java.sql.Date之间的转换
和 java.sql.Time
以及上面的四个类(双向)?
Does jOOQ support translation between java.sql.Timestamp
, java.sql.Date
and java.sql.Time
and the four classes above (in both directions)?
推荐答案
jOOQ路线图
直到jOOQ 3.6,JSR-310日期时间类型没有官方支持,因为jOOQ开源版和商业版仍然支持Java 6。
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.
使用jOOQ 3.7,由于Java 6仅在专门构建的商业发行版中受支持,因此更改,而标准发行版将需要Java 8。此更改的相关问题是#4338 。
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.
另一个更改涉及一个标志< javaTimeTypes />
,告诉源代码生成器生成JSR-310类型而不是 java.sql.Date
和类似的类型。默认情况下,此标志设置为false,因此您需要将其设置为true,直到发出# 5714 已解决。
Another change involves a flag <javaTimeTypes/>
to tell the source code generator to generate JSR-310 types rather than java.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.
同时,您可以自己绑定所需的类型,并让代码生成器使用您的自定义转换器
或绑定
。请参阅手册中的相关章节:
In the meantime, you can bind the desired types yourself and let the code generator use your custom Converter
or Binding
. See the relevant sections in the manual:
- http://www.jooq.org/doc/latest/manual/sql-building/queryparts/custom-bindings
- http:// www.jooq.org/doc/latest/manual/code-generation/custom-data-types
- http://www.jooq.org/doc/latest/manual/code-generation/custom-data-类型绑定
- http://www.jooq.org/doc/latest/manual/sql-building/queryparts/custom-bindings
- http://www.jooq.org/doc/latest/manual/code-generation/custom-data-types
- http://www.jooq.org/doc/latest/manual/code-generation/custom-data-type-bindings
< a href =http://download.oracle.com/otndocs/jcp/jdbc-4_2-mrel2-eval-spec/index.html =nofollow noreferrer> JDBC 4.2规范定义了哪个JSR-应该将310种数据类型映射到哪些JDBC数据类型(这与PostgreSQL的结合一致)相同的)。特别是,JDBC规范列表:
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:
- 添加了对map
java.time.LocalDate $ c $的支持c>到JDBC
DATE
。 - 添加了对map
java.time.LocalTime $ c $的支持c>到JDBC
TIME
- 添加了对map
java.time.LocalDateTime
到JDBCTIMESTAMP
。 - 添加了对map
java.time.OffsetTime
到JDBCTIME_WITH_TIMEZONE
。 - 添加了对map
java.time.OffsetDateTime
到JDBCTIMESTAMP_WITH_TIMEZONE
。
- Added support to map
java.time.LocalDate
to JDBCDATE
. - Added support to map
java.time.LocalTime
to JDBCTIME
- Added support to map
java.time.LocalDateTime
to JDBCTIMESTAMP
. - Added support to map
java.time.OffsetTime
to JDBCTIME_WITH_TIMEZONE
. - Added support to map
java.time.OffsetDateTime
to JDBCTIMESTAMP_WITH_TIMEZONE
.
JDBC的理解似乎并不完全正确,因为很少有数据库真正将时区信息存储在 TIMESTAMP WITH TIME ZONE
数据类型中(例如Oracle确实如此),另请参阅此DBA / StackExchange答案: https://dba.stackexchange.com/a/59021/2512 。由于未存储时区,因此将 java.time.Instant
映射到 TIMESTAMP WITH TIME ZONE
的方法更好,因为SQL类型实际上只是一个UTC时间戳。
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 mapping java.time.Instant
to TIMESTAMP WITH TIME ZONE
is better, because the SQL type is really just a UTC timestamp.
尽管如此, OffsetDateTime
仍然可以映射到 TIMESTAMP WITH TIME ZONE
,但是你不知道时间戳存储在PostgreSQL中时使用了什么时区。
Having said so, OffsetDateTime
can still be mapped to TIMESTAMP WITH TIME ZONE
, but you will not know what time zone was used when the timestamp was stored in PostgreSQL.
这篇关于jOOQ - 支持JSR310的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!