I have a JPA entity class with an OffsetDateTime
field like this:
@Entity
class MyEntity(clock: Clock) {
// ...
val created: OffsetDateTime = OffsetDateTime.now(clock)
}
In a repository test I check that the date is corretly mapped:
assertThat(entity.created).isEqualTo(defaultDateTime)
If I compare the underlying Instant
(and not also the offset) with OffsetDateTime#isEqual
it works:
assertThat(entity.created.isEqual(defaultDateTime)).isTrue()
defaultDateTime
and the clock
are created with this code:
val defaultDateTime: OffsetDateTime =
OffsetDateTime.of(2019, 6, 15, 11, 48, 59, 0, ZoneOffset.UTC)
fun createTestClock(dateTime: OffsetDateTime = defaultDateTime) =
Clock.fixed(dateTime.toInstant(), ZoneId.of("UTC"))
But if I run my test, the time differs by exactly 2 hours (what is the actual offset in my region, central europe). The PostgreSQL database returns GMT
if I query the timezone with show timezone;
.
The column data type is:
timestamp with time zone not null
Setting the respective Hibernate property doesn't help:
spring.jpa.properties.hibernate.jdbc.time_zone=UTC
What is wrong?
timestamp with time zone not null
.show time zone
all the time or just when debugging? TwoOffsetDateTime
s are equal only if offsets are also equal.java.sql.Timestamp
intoZonedDateTime
(insideorg.hibernate.type.descriptor.java.ZonedDateTimeJavaDescriptor
) is implemented astimestamp.toLocalDateTime().atZone(ZoneId.systemDefault())
. I suspect this might explain whyhibernate.jdbc.time_zone
gets ignored