如何通过Hibernate以UTC格式存储时间戳?

时间:2017-02-07 13:30:36

标签: hibernate utc

目前我根据sysdate存储时间戳,但我希望以UTC格式保存时间戳。 PFB是我的实体类中的代码片段。

@Temporal(TemporalType.TIMESTAMP) 
    @Column(name = "CREATE_DT")
    private Date createDt = new Date(); 

如何以UTC格式存储时间戳。

1 个答案:

答案 0 :(得分:0)

只需将JDBC选项更改为JDBC连接URL:
这些是您需要更改的参数:

  

useTimezone =真
  serverTimezone = UTC

实施例

<bean id="HB_SessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="hibernateProperties">
    <props>
        <!-- Connection parameters -->
        <prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop>
        <prop key="hibernate.connection.url">jdbc:mysql://hostname/databaseName?useTimezone=true&amp;serverTimezone=UTC</prop>
        ...
相关问题