hibernate.jdbc.time_zone = UTC ignored

后端 未结 3 2222
我寻月下人不归
我寻月下人不归 2021-02-15 13:02

Using set up as

  • Spring framework 5.0.2.RELEASE
  • Spring Security 5.0.0.RELEASE
  • Hibernate 5.2.11.Final

I followed spring boot &

相关标签:
3条回答
  • 2021-02-15 13:05

    Try this(it worked for me). Write below code snippet in your spring boot main application file.

    @PostConstruct
    public void started() {
        TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
    }
    

    Source

    0 讨论(0)
  • 2021-02-15 13:10

    I think you should put spring.jpa.properties.hibernate.jdbc.time_zone = UTC in application.yml/application.properties of main/ or test/, not in db.properties. And, I think .properties takes precedence over .yml. Correct me if I am wrong.

    And, this timezone config is also affected by Timezone.getDefault().

    See this post: https://aboullaite.me/spring-boot-time-zone-configuration-using-hibernate/

    And, as I test, I see that if you want to save a java.util.Date, which has no timezone info in itself(only in its Calendar property, this is true), and the column definition hold no place for timezone info, this property will affect the value saved to DB, but not that when you retrieve the row from DB; the latter is affected only by Timezone.getDefault(). Meanwhile, if you set Timezone.getDefault(), the saved and retrieved value will both be the timezone you want.

    So, this property may not be the most proper way to manipulate the value of Date, if you save and query for the information in the same application. Just use Timezone.setDefault(Timezone.getTimezone("XXX")).

    0 讨论(0)
  • 2021-02-15 13:26

    Change this:

     properties.put("spring.jpa.properties.hibernate.jdbc.time_zone", 
                    environment.getRequiredProperty("spring.jpa.properties.hibernate.jdbc.time_zone"));
    

    to this:

     properties.put("hibernate.jdbc.time_zone", 
                environment.getRequiredProperty("spring.jpa.properties.hibernate.jdbc.time_zone"));
    
    0 讨论(0)
提交回复
热议问题