How to persist LocalDate and LocalDateTime with JPA

Опубликовано: 01 Октябрь 2024
на канале: Thorben Janssen
14,275
165

Join the free Member Library to get access to free ebooks and cheat sheets about JPA and Hibernate:
https://goo.gl/dfqr6n

Java 8 brought lots of great features, and one of the most important and most anticipated ones was the new Date and Time API. There were lots of issues with the old API, and I won’t get into any details on why we needed a new one. I’m sure you had to struggle often enough with it yourself.

The new Date and Time API is much better. It’s well designed, easy to use and (finally) immutable. The only issue that remains is that the JPA standard doesn’t support it.
Well, that’s not completely correct. You can use it, but JPA will map it to a BLOB instead of a DATE or TIMESTAMP. That means the database is not aware of the date object and cannot apply any optimization for it. That’s not the way we should or want to do it.

But that doesn’t mean that you can’t use the Date and Time API. You just have to decide how you want to add the support for it. You either use Hibernate 5, which provides proprietary support for the Date and Time API, or you take a few minutes to implement an AttributeConverter like I show you in this video.

Read the original post: https://www.thoughts-on-java.org/pers...

If you like this video, please give me your thumbs up and share it with your friends and co-workers.

Like my channel? Subscribe!
➜ http://bit.ly/2cUsid8

Want to connect with me?
Blog: http://www.thoughts-on-java.org/
Twitter:   / thjanssen123  
Facebook:   / thoughtsonjava  
#JPA #Hibernate #Java #DateTimeAPI