How to persist LocalDateTime & Co with Hibernate 5

Опубликовано: 07 Октябрь 2024
на канале: Thorben Janssen
2,687
34

Do you use Java 8’s Date and Time API in your projects? Let’s be honest, working with java.util.Date is a pain, and I would like to replace it with the new API in all of my projects.
The only problem is that JPA does not support it.
Java 8 was released after JPA 2.1, and the persistence standard does not support the new APIs. You can, of course, use LocalDate or other classes of the Date and Time API as entity attributes, but you can’t annotate them with @Temporal and Hibernate stores them as blobs in the database.
You have 2 options, if you want to use the right JDBC types when you persist these classes:
You can implement a JPA AttributeConverter as I showed you in one of the previous videos.
 Or you can use the Hibernate-specific Java 8 support which was introduced with Hibernate 5. This approach is not portable to other JPA implementations but much easier to use as I will show you in this video.

Links mentioned in the video:
How to persist LocalDate and LocalDateTime with JPA:    • How to persist LocalDate and LocalDat...  

Original Post: https://thoughts-on-java.org/hibernat...

Join the free Thoughts on Java Library: https://goo.gl/OSG8rH

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

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