BobJI
BobJI

Reputation: 173

Hibernate with java.time

I'm using Hibernate like this:

 <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.2.12.Final</version>
 </dependency>

I read that since Hibernate 5.2, there's native support for java.time.

My entity field is still a java.util.Date, and I wanted to find entities using a LocalDateTime.

However I get an exception InvalidDataAccessApiUsageException with the message "Parameter value [2020-02-26T00:00] did not match expected type java.util.date".

Is it not supposed to convert the LocalDateTime automatically ?

Upvotes: 0

Views: 118

Answers (1)

The Next Programmer
The Next Programmer

Reputation: 83

Is it not supposed to convert the LocalDateTime automatically ?

No, there is no magic behind the covers that will cast a java.util.Date object to java.time.LocalDateTime, or vice-versa- automatically.
Now, you could manually convert the objects yourself, but I would advise against doing that. it's considered good practice to use the same data type throughout all layers of your application.

Sidenote: Keep in mind java.util.Date is an older class and does not factor in a lot of things (for example- leap years). LocalDateTime on the contrary comes with a lot more features and is very robust. It all just depends on what your use case scenario is. I suggest reading documentation for both the classes to determine which one is more suitable for you. Also, this answer does a great job at comparing most date-time types in Java

Upvotes: 1

Related Questions