Search code examples
javahibernatehibernate-5.xlocaldate

Use java.time in Hibernate 5.1


I'm using Hibernate 5.1 in my application. Now I want to convert all the java.util.date classes to the java.time java 8 classes. I changed all my source code to the new Java 8 standard. But when I query the first query that contains a mapping to a LocalData it throws an error:

java.lang.ClassCastException: java.sql.Date cannot be cast to java.time.LocalDate

The Hibernate-java8 jar is in my classpath.

Can someone help me to use java.time classes instead of the old java.util.date? Do I need to configure something in my Hibernate config for it?


Solution

  • I think the problem is not about the support of LocaleDate by Hibernate 5.1 the exception says that there's a cast Exception from java.sql.Date to java.time.LocaleDate.

    If you check the Hibernate 5.1 User Guide you can see that Hibernate provides a replamcement type for the java.time.LocaleDate which is LocalDateType class.

    So instead of using java.time.LocaleDate you shoud use org.hibernate.type.LocalDateType in your entities.