1

I have a model object where I want to have a field with a date. For now I am using ZonedDateTime as it fits our needs.

Hibernate stores this field in the database as a tinyblob. Can we change the way it is saved in the database to a more readable format and more importantly to a sortable format?

The database used is a mysql db.

Panos
  • 7,227
  • 13
  • 60
  • 95
  • I assume by "sortable" you mean "sortable by instant-in-time"? (`ZonedDateTime` is a bad fit if you really only want a date, btw. `LocalDate` would be the right type for that.) – Jon Skeet May 10 '17 at 15:51
  • @JonSkeet I am using ZonedDateTime in fields where I need the exact time up to milliseconds. LocalDate in fields that I need date only. It would be an overkill to mention both in this thread – Panos May 10 '17 at 19:47
  • "It would be an overkill to mention both in this thread." I really don't think it's "overkill" to ask you to accurately describe the data you're trying to represent. You said you're trying to represent a date, but you're not. You're trying to represent an instant in time along with an associated time zone, presumably. When you're trying to receive help, it's important to be as accurate as you can be. – Jon Skeet May 10 '17 at 21:36

1 Answers1

3

This issue has already been resolved as a new improvement to Hibernate 5.0.0.Beta1, originally it was bundled in an isolated module hibernate-java8

Now it is directly bundled in hibernate-core, so just make sure that you are using the recent version of it (5.2.X +), in case you are using maven, it should be like this

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-core</artifactId>
  <version>5.2.10.Final</version>
</dependency>
fujy
  • 5,168
  • 5
  • 31
  • 50
  • Thanks. I will give it a try and let you know :) – Panos May 10 '17 at 19:47
  • I tried the solution. The hibernate core solution has a conflict with spring data. It needs jpa 2 while spring data works with jpa and the application does not even start. I also tried the hibernate-java8 version but it didn't fix it. – Panos May 11 '17 at 10:13
  • @Panos then you will have to upgrade your Spring Data version too, at least to version `1.11` that supports Hibernate 5.2, check here http://docs.spring.io/spring-data/jpa/docs/2.0.0.M3/reference/html/#new-features.1-11-0 – fujy May 11 '17 at 10:20
  • @fuji After changing all my dependencies (I removed the `spring-boot-starter-jpa` and manually added the spring data and hibernate dependencies) I managed to make it work BUT it saves it as `TIMESTAMP` and when I save a `ZonedDateTime` it ignores the timezone. Any ideas? – Panos May 30 '17 at 15:10
  • @Panos Check [this](https://stackoverflow.com/questions/4562456/mysql-setting-time-zone-in-my-cnf-options-file) – fujy May 30 '17 at 15:23
  • Upgrading to hibernate core 5.2.10.Final worked great for me. Using spring boot 1.5.4.RELEASE and spring data 1.5.4.RELEASE. – teuber789 Jul 02 '17 at 21:59