I have a running app spring boot 1.3 + hibernate 5 + java 8 + ZonedDateTime + postgresql and in one of the tables I have the following fields.
@Column(name = "DATE_ENABLED")
@Type(type="java.time.ZonedDateTime")
private ZonedDateTime dateEnabled;
@Column(name = "DATE_DISABLED")
@Type(type="java.time.ZonedDateTime")
private ZonedDateTime dateDisabled;
If I run the app then I see that this by default produces "timestamp without time zone"
testDB=# \d type
Table "public.type"
Column | Type | Modifiers
--------------------------------+-----------------------------+-----------
type_id | bytea | not null
date_disabled | timestamp without time zone |
date_enabled | timestamp without time zone |
I know that if I add the columnDefinition= "TIMESTAMP WITH TIME ZONE" to the column i.e. something like
@Column(name = "DATE_DISABLED", columnDefinition= "TIMESTAMP WITH TIME ZONE")
then it works correctly and I am able to see that hibernate created a column with time zone, but if I understand this correctly this will be only working for postgres i.e. if I change the database tomorrow to mysql then hibernate will throw an error.
Thus my question is how to do that in general i.e. to tell hibernate to create a column that should include the time zone and the offset. I was of the opinion that since the java type "ZonedDateTime" is deliberately created to include the time zone and the time offset in UTC then hibernate will by default create a column that include the time zone. Thus the question again: what is the proper way to tell hibernate to include the time zone and the offset.
Here are parts of my pom:
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.0.RELEASE</version>
<hibernate.version>5.0.4.Final</hibernate.version>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-java8</artifactId>
<version>${hibernate.version}</version>
</dependency>
and my properties file showing the dialect
@Bean
public Properties hibernateProperties() {
return new Properties() {
{
setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
setProperty("hibernate.chach.provider_class", "org.hibernate.cache.NoCacheProvider");
setProperty("hibernate.show_sql", "true");
setProperty("hibernate.hbm2ddl.auto", "create-drop");
}
};
}
timestamp without time zone
is incorrect for storing a value ofZonedDateTime
. Should be a column of typetimestamp with time zone
to store a moment, a specific point on the timeline as represented in aZonedDateTime
which Postgres adjusts to UTC for storage. Lacking any concept of time zone or offset-from-UTC, atimestamp without time zone
cannot store a moment. The column typetimestamp without time zone
maps to Java typeLocalDateTime
.