6

I am using Hibernate and JPA to store my data in a database. Now I would like to save a ZonedDateTime from java.time.

The problem is, Hibernate does only ever persist the date and time. It does not care about the ZoneId or the offset.

Is there any way to persuade Hibernate to persist that information?

Mr.H.
  • 965
  • 1
  • 10
  • 18
  • Do you try to save it as a TimeStamp in the database? – Robin Topper Aug 14 '17 at 13:55
  • 1
    Which database engine are you using? MySQL, for example, can't store the zone, you have to do it yourself. – Guillaume F. Aug 14 '17 at 16:22
  • I can confirm this issue with `postgres 9.5.10` and `hibernate 5.2.12`. The automatically created column type is `timestamp without timezone` whereas I would expect it to be `timestamp with timezone` – ST-DDT Jan 02 '18 at 12:54
  • Related: https://stackoverflow.com/questions/34008907/hibernate-5-zoneddatetime-postgresql-inlcude-time-zone-and-the-offset – ST-DDT Jan 02 '18 at 12:57

1 Answers1

4

hibernate 5 support java8 datetime types by adding hibernate-java8 dependancy but in version 5.2.10 this moved to hibernate core

(deprecated - use hibernate-core instead) Support for Java8-specific features - mainly Java8 Date/Time (JSR 310)

<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-java8 -->
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-java8</artifactId>
</dependency>

for more information about hibernate and datetime see this

import java.time.Instant;

@Column(name = "reset_date")
private Instant resetDate = null;


setResetDate(Instant.now());

https://www.thoughts-on-java.org/hibernate-5-date-and-time/

ali akbar azizkhani
  • 2,213
  • 5
  • 31
  • 48