Spring Boot + Spring Data JPA + Java 8 date and time (JSR310)

In Spring Boot + Spring Data JPA application, to support the JSR310 java.time.* APIs, we need to register this Jsr310JpaConverters manually.


import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.data.jpa.convert.threeten.Jsr310JpaConverters;

@EntityScan(
        basePackageClasses = {Application.class, Jsr310JpaConverters.class}
)
@SpringBootApplication
public class Application {
	//...
}

P.S Tested with Spring Boot 1.5.1.RELEASE, Spring Data JPA 1.11.0.RELEASE

1. Full Example

1.1 A model contains a java.time.LocalDate field.


package com.mkyong.model;

import javax.persistence.*;
import java.time.LocalDate;

@Entity
public class Customer {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "CUST_SEQ")
    @SequenceGenerator(sequenceName = "customer_seq", allocationSize = 1, name = "CUST_SEQ")
    Long id;

    String name;

    @Column(name = "CREATED_DATE")
    LocalDate date;
	
	//...

1.2 @EntityScan to scan and register Jsr310JpaConverters like this :


package com.mkyong;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.data.jpa.convert.threeten.Jsr310JpaConverters;

import java.util.Arrays;

//for jsr310 java 8 java.time.*
@EntityScan(
        basePackageClasses = {Application.class, Jsr310JpaConverters.class}
)
@SpringBootApplication
public class Application {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public CommandLineRunner run(ApplicationContext appContext) {
        return args -> {

            System.out.println("hello World!");
            
        };
    }

}

References

  1. Jsr310JpaConverters JavaDoc
  2. JSR 310: Date and Time API
  3. Spring Data MongoDB + JSR-310 or Java 8 new Date APIs

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
5 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
5 Comment authors
naveenchethan kumarjaniceJakub Pomyka?aRajesh kumar Recent comment authors
newest oldest most voted
Rajesh kumar
Guest
Rajesh kumar

very helpful

janice
Guest
janice

This did not work for me. I still get error: java.sql.SQLException: Bad format for DATE ’10/13/2015 12:00:00 AM’ in column 2. import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.support.SpringBootServletInitializer; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.PropertySource; import org.springframework.data.jpa.convert.threeten.Jsr310JpaConverters; @EntityScan( basePackageClasses = {MehApplication.class, Jsr310JpaConverters.class} ) @SpringBootApplication @PropertySource( { “classpath:/meh_application.properties” }) @EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class }) @ComponentScan( { “com.spins.productlibrary.environment”, “com.spins.productlibrary.service”, “com.spins.productlibrary.model” }) public class MehApplication extends SpringBootServletInitializer { public static void main(String[] args) { TimeZone.setDefault(TimeZone.getTimeZone(“Etc/UTC”)); ApplicationContext ac = SpringApplication.run(MehApplication.class, args); Logger logr = LoggerFactory.getLogger(MehApplication.class); logr.debug(“Beans provided by Spring Boot:”); String[] beanNames = ac.getBeanDefinitionNames(); Arrays.sort(beanNames); for (String beanName : beanNames) { logr.debug(“Bean provided… Read more »

chethan kumar
Guest
chethan kumar

hi sir its nice, you r giving your knowledge for free thank you for that but i need some exaples on jsf its hard to find jsf work online explained like you.

naveen
Guest
naveen

Spring Data JPA save(Iterable ….) exampl

Jakub Pomyka?a
Guest
Jakub Pomyka?a

I think it’s way better to just change name of ‘LocalDate date’ to ‘LocalDate createdDate’ and Spring Boot will create column in database named ‘created_date’ 🙂