mCs
mCs

Reputation: 2921

Use newer hibernate or JPA 2.2 for Java 8 Date/Time in SPRING-BOOT?

Using spring-boot 1.5.10 (so the current) to use the Java8 Date/Time is NOT supported as it looks like by default this version of Spring Boot uses Hibernate 5.0.12.Final. To achieve this (Spring Boot 1.5.10) in the pom.xml properties section overriding the version works.

<hibernate.version>5.2.12.Final</hibernate.version>

Question: What is the best way to enable Java8 Date/Time to override hibernate version or somehow (if so then how?) use the JPA 2.2 for SPRING_BOOT?

EDIT With hibernate 5.0.x I keep getting:

Caused by: org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #1 of URL [file:./src/main/resources/db/h2/data-h2.sql]: INSERT INTO reservations (ID, RESERVATION_NAME, DATE_FROM, DATE_TO) VALUES (1, 'res1', ParseDateTime('01 01 2001', 'd M yyyy'), ParseDateTime('02 01 2001', 'd M yyyy')), (2, 'res1', ParseDateTime('01 02 2001', 'd M yyyy'), ParseDateTime('02 02 2001', 'd M yyyy')), (3, 'res3', ParseDateTime('01 03 2001', 'd M yyyy'), ParseDateTime('02 03 2001', 'd M yyyy')), (4, 'res4', ParseDateTime('01 04 2001', 'd M yyyy'), ParseDateTime('02 04 2001', 'd M yyyy')), (5, 'res5', ParseDateTime('01 05 2001', 'd M yyyy'), ParseDateTime('02 05 2001', 'd M yyyy')); nested exception is org.h2.jdbc.JdbcSQLException: Heksadecymalny string z nieparzystą liczbą znaków: "2001-01-01 00:00:00.0" Hexadecimal string with odd number of characters: "2001-01-01 00:00:00.0"; SQL statement: INSERT INTO reservations (ID, RESERVATION_NAME, DATE_FROM, DATE_TO) VALUES (1, 'res1', ParseDateTime('01 01 2001', 'd M yyyy'), ParseDateTime('02 01 2001', 'd M yyyy')),

but when using the 5.2.12.Final everything is working .

Upvotes: 1

Views: 4819

Answers (3)

jvleminc
jvleminc

Reputation: 81

I had the same issue and playing around with spring-boot-parent (finally opted using latest 1.5.x version: 1.5.16.RELEASE) and spring-boot-starter-data-jpa versions didn't help.

What did help in the end, was adding the dependency on hibernate-java8, with a 5.1.X version. As OP indicates correctly, spring boot by default already brings this dependency, but with a lower (==hibernate.version) version.

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-java8</artifactId>
    <version>5.1.0.Final</version><!--$NO-MVN-MAN-VER$-->
</dependency>

Upvotes: 0

canillas
canillas

Reputation: 424

One of the features added with Hibernate 5 is the support of Java 8 classes like the Date and Time API, so you can add the .jar to your pom.xml:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-java8</artifactId>
    <version>5.1.0.Final</version>
</dependency>

Upvotes: 1

Dimitri Mestdagh
Dimitri Mestdagh

Reputation: 44755

Hibernate 5.x already had support for the Java 8 Date/Time API, but within a separate artifact called hibernate-java8.

Using Maven, you can include it like this:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-java8</artifactId>
    <version>5.0.12.Final</version>
</dependency>

However, since Hibernate 5.2.x, the baseline Java version for Hibernate is Java 8, so there was no longer a need to keep a separate module for the Java 8 date/time support. That's why it was now merged into hibernate-core, as documented in the migration guide:

Hibernate 5.2 is built using Java 8 JDK and will require Java 8 JRE at runtime (we are investigating whether Java 9 will also work). This has a number of implications:

  • The hibernate-java8 module has been merged into hibernate-core and the Java 8 date/time types are now natively supported.

So, to answer your question, for Hibernate 5.0.x and Hibernate 5.1.x, the official way to support the new date/time API is to use the hibernate-java8 module.

Upvotes: 7

Related Questions