Search code examples
spring-boothibernatejdbcspring-data-jpaspring-data

hibernate.jdbc.time_zone property of spring boot config does not work in spring version 3.2.1


My Spring application timezone is UTC corresponding to the following config:

TimeZone.setDefault(TimeZone.getTimeZone("UTC"));

My hibernate JDBC timezone configured to Asia/Tehran in application.yaml of spring boot:

spring.jpa.properties.hibernate.jdbc.time_zone = Asia/Tehran

Before upgrading to new version of Spring, everything was working fine. When I was storing date field with spring repository, dates stored in local timezone (Asia/Tehran). Sample code:

basketEntity.setModifiedDateTime(ZonedDateTime.now());
basketRepository.save(basketEntity);

In above code ZonedDateTime.now() is GMT time because of application timezone and hibernate convert it to local zone.

But after upgrading spring boot from version 3.0.4 to 3.2.1 and accordingly hibernate ORM versions from 6.1.7.Final to 6.4.1.Final, all dates stored on GMT and apparently hibernate.jdbc.time_zone config does not work as I expect anymore. I have confirmed that hibernate timezone config applied in spring boot startup log.

Note that My date field in entity is like following:

@Column(name = "modified_date_time", insertable = false, columnDefinition = "datetime2")
private ZonedDateTime modifiedDateTime;

And My database is SQL Server 2019 and date field type in DB is datetime2

An example of my date field data stored in DB:

2024-01-09 10:52:31.6832713

p.s. It seems that JVM override timezone behavior of jdbc config in latest version of hibernate.

(I followed this link for my application setup) Hibernate retrieve timestamps in UTC


Solution

  • I solved the problem with setting a new field in my config in new version:

    Set hibernate.timezone.default_storage to NORMALIZE

    I find the solution in migration guide of hibernate:

    6.2 Migration Guide of Hibernate