Skip to content

ZonedDateTime emulation

Philippe Marschall edited this page Jun 8, 2017 · 13 revisions

A lot of JDBC drivers only support OffsetDateTime and not ZonedDateTime. However ZonedDateTime support can be emulated using a TIMESTAMP WITH TIME ZONE column holding the timestamp and a VARCHAR field holding the zone id.

The threeten-jpa-zoned-hibernate provides the ZonedDateTimeType Hibernate composite type which maps two such columns to a ZonedDateTime field.

Set up

You need two columns to hold the timestamp in UTC and the zone id. On our JVM the longest zone id fits in VARCHAR(32) but you can also make the column wider

  TIMESTAMP_UTC TIMESTAMP WITH TIME ZONE,
  ZONE_ID VARCHAR(32),

On the Java side you have to set the type an map both columns

  @Type(type = ZonedDateTimeType.NAME)
  @Columns(columns = {
      @Column(name = "TIMESTAMP_UTC"),
      @Column(name = "ZONE_ID")
  })
  private ZonedDateTime zonedDateTime;

It is important that the TIMESTAMP WITH TIME ZONE column is the first one and the VARCHAR is the second one.

Examples

Have a look at the schema sample and the mapping example on how to use it.

Clone this wiki locally