How to persist JSR-310 types with Spring Data JPA?

UPDATE: The answer below is valid if you need to stay on a Hibernate version < 5.0. Hibernate 5.0 supports persisting JSR-310 date/time types out of the box. I.e. if you are on Hibernate 5.0 or newer, Adam’s answer is the way to go. Everyone else, read on.

The root cause for this at none of the widely used JPA providers actually support JSR-310 types out of the box yet. However, as of Spring Data JPA 1.8.0 we ship JPA 2.0 converters that will translate non-time-zoned JSR-310 types into a legacy Date so that they can be persisted as is.

To get this working, simply register org.springframework.data.jpa.convert.threeten.Jsr310JpaConverters as one of the managed JPA classes with your provider. There’s a couple of way to do that: in a very standard JPA setup you list it in your persistence.xml. In a LocalContainerEntityManagerFactoryBean based setup you can just add the package of the class to the packagesToScan property. If you’re using Spring Boot adding the class to the @EntityScan annotation does the trick.

The latter is described in a bit more detail in the blog post covering the new features the Spring Data release train named Fowler ships

Leave a Comment