java8 OffsetDateTime 和 Timestamp 之间的 Scala Slick 3.0 隐式映射
Scala Slick 3.0 implicit mapping between java8 OffsetDateTime and Timestamp
首先,如果我想将带时区的日期时间映射到 Slick,我应该使用 OffsetDateTime
还是 ZonedDateTime
哪个 class?至于Joda,我们只能用DateTime
.
如何为 Slick table 映射编写一些隐式代码以在 java8 ZonedDateTime
和 Sql Timestamp
之间进行转换?
使用 joda DateTime
来包含时区信息似乎非常简单。然而,一旦切换到 Java8,我不太确定我应该使用 ZonedDateTime
还是 OffsetDateTime
,因为 http://www.oracle.com/technetwork/articles/java/jf14-date-time-2125367.html 建议使用 OffsetDateTime。
对于我当前的代码,我只使用 Java8 LocalDateTime
并且我编写了以下隐式来在 slick 之间进行映射。
implicit val JavaLocalDateTimeMapper = MappedColumnType.base[LocalDateTime, Timestamp](
l => Timestamp.valueOf(l),
t => t.toLocalDateTime
)
不太确定我是否可以使用 ZonedDateTime
或 OffsetDateTime
编写类似的内容?
简答
从 Slick 3.1 开始,简短的回答是使用 OffsetDateTime,但您需要将其映射到 String
列,而不是 Timestamp
才能正常工作与任何数据库。
也就是说,您需要 MappedColumnType.base[OffsetDateTime, String]
。您可以使用 toString
和 OffsetDateTime.parse
进行字符串转换:
scala> import java.time._
import java.time._
scala> val paris = ZoneId.of("Europe/Paris")
paris: java.time.ZoneId = Europe/Paris
scala> OffsetDateTime.now(paris)
res0: java.time.OffsetDateTime = 2016-01-05T20:38:46.473+01:00
scala> OffsetDateTime.parse(res0.toString)
res2: java.time.OffsetDateTime = 2016-01-05T20:38:46.473+01:00
更长的答案
OffsetDateTime 和 ZonedDateTime 的区别在 ,这里不再赘述
但是,您需要阅读它以确定简短答案是否符合您的情况。
如果您使用 Postgres,there's support for java.time via the slick-pg project. I've not had the chance to use it myself, but clearly worth investigating if that's the database you're using. See e.g., the test suite 用于 "date2" add-on。
更好的答案(或未来的答案!)
好消息是,有一个活跃的拉取请求要在 Slick 中添加对 java.time 数据类型的支持。您可以监控进度 on the ticket,目前为 Slick 3.2 排定。
有一个我认为从未发布过的叉子,但它确实有 Java8 时间支持:
https://github.com/xavier-fernandez/slick
它非常稳定,我一直在产品中将它与 HSQLDB 一起使用。以及相关的 specification/pull 请求:
https://github.com/slick/slick/pull/1349
如果您正在寻找代码生成,这并不太难,使用 above-mentioned 分支,这个 post 描述了如何做:
https://github.com/slick/slick/pull/1349#issuecomment-245566307
首先,如果我想将带时区的日期时间映射到 Slick,我应该使用 OffsetDateTime
还是 ZonedDateTime
哪个 class?至于Joda,我们只能用DateTime
.
如何为 Slick table 映射编写一些隐式代码以在 java8 ZonedDateTime
和 Sql Timestamp
之间进行转换?
使用 joda DateTime
来包含时区信息似乎非常简单。然而,一旦切换到 Java8,我不太确定我应该使用 ZonedDateTime
还是 OffsetDateTime
,因为 http://www.oracle.com/technetwork/articles/java/jf14-date-time-2125367.html 建议使用 OffsetDateTime。
对于我当前的代码,我只使用 Java8 LocalDateTime
并且我编写了以下隐式来在 slick 之间进行映射。
implicit val JavaLocalDateTimeMapper = MappedColumnType.base[LocalDateTime, Timestamp](
l => Timestamp.valueOf(l),
t => t.toLocalDateTime
)
不太确定我是否可以使用 ZonedDateTime
或 OffsetDateTime
编写类似的内容?
简答
从 Slick 3.1 开始,简短的回答是使用 OffsetDateTime,但您需要将其映射到 String
列,而不是 Timestamp
才能正常工作与任何数据库。
也就是说,您需要 MappedColumnType.base[OffsetDateTime, String]
。您可以使用 toString
和 OffsetDateTime.parse
进行字符串转换:
scala> import java.time._
import java.time._
scala> val paris = ZoneId.of("Europe/Paris")
paris: java.time.ZoneId = Europe/Paris
scala> OffsetDateTime.now(paris)
res0: java.time.OffsetDateTime = 2016-01-05T20:38:46.473+01:00
scala> OffsetDateTime.parse(res0.toString)
res2: java.time.OffsetDateTime = 2016-01-05T20:38:46.473+01:00
更长的答案
OffsetDateTime 和 ZonedDateTime 的区别在
但是,您需要阅读它以确定简短答案是否符合您的情况。
如果您使用 Postgres,there's support for java.time via the slick-pg project. I've not had the chance to use it myself, but clearly worth investigating if that's the database you're using. See e.g., the test suite 用于 "date2" add-on。
更好的答案(或未来的答案!)
好消息是,有一个活跃的拉取请求要在 Slick 中添加对 java.time 数据类型的支持。您可以监控进度 on the ticket,目前为 Slick 3.2 排定。
有一个我认为从未发布过的叉子,但它确实有 Java8 时间支持:
https://github.com/xavier-fernandez/slick
它非常稳定,我一直在产品中将它与 HSQLDB 一起使用。以及相关的 specification/pull 请求:
https://github.com/slick/slick/pull/1349
如果您正在寻找代码生成,这并不太难,使用 above-mentioned 分支,这个 post 描述了如何做:
https://github.com/slick/slick/pull/1349#issuecomment-245566307