JDK1.8 环境下使用@Entity注解的类日期时间在数据库表中不能正常生成

JDK1.8 环境下,使用SpringBoot + SpringDataJPA+Mysql时,使用@Entity注解的实体类中,日期时间跟预期不符;

//活动日期
private LocalDate activityDate;

@Column(nullable = false)
private LocalTime startTime;

@Column(nullable = false)
private LocalTime endTime;

//创建时间
private LocalDateTime createTime = LocalDateTime.now();

本来预想 activityDate 是 date类型,startTime和 endTime是time类型,createTime 是 dateTime类型,但是生成表之后才发现都是 tinyblob 类型的。

JDK1.8 环境下使用@Entity注解的类日期时间在数据库表中不能正常生成_第1张图片

很是郁闷,心想是不是JDK1.8的日期时间用不成了,不能啊,那代码都是大牛写的,又不是我写的。

后来找到解决方法了:

  1.检查JDK环境,务必保证版本高于1.8;

  2.添加依赖:


  org.hibernate
  hibernate-java8
  5.2.17.Final

    然后就可以愉快的在Entity中是用JDK1.8 中的日期时间了:

    JDK1.8 环境下使用@Entity注解的类日期时间在数据库表中不能正常生成_第2张图片

  如果加了hibernate-java8依赖不起作用,我用的是SpringBoot + SpringDataJpa 是1.5.9的版本,就没起作用,然后升级 hibernate-core 到更高级的版本:


  org.hibernate
  hibernate-core
  5.2.17.Final

表生成了,但是启动应用时报错:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: org.hibernate.engine.spi.SessionFactoryImplementor.getProperties()Ljava/util/Properties;
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
	at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1080)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:857)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
	at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)
	at com.mdvns.mdvn.staff.MdvnStaffApplication.main(MdvnStaffApplication.java:15)
Caused by: java.lang.NoSuchMethodError: org.hibernate.engine.spi.SessionFactoryImplementor.getProperties()Ljava/util/Properties;
	at org.hibernate.jpa.internal.EntityManagerFactoryImpl.(EntityManagerFactoryImpl.java:124)
	at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:890)
	at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60)
	at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:360)
	at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:382)
	at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:371)
	at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:336)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624)
	... 16 common frames omitted

这是因为SpringDataJPA中本来就有hibernate-core的依赖包,我们又新加了更高版本的包后冲突了,所以只需要让SpringDataJPA依赖新加的高版本hibernate-core就可以了:


  org.springframework.boot
  spring-boot-starter-data-jpa
  
    
      org.hibernate
      hibernate-core
    
    
      org.hibernate
      hibernate-entitymanager
    
  

到此,SpringBoot + SpringDataJPA的实体类使用jdk1.8 中的日期时间类型直接生成表就没问题了:

JDK1.8 环境下使用@Entity注解的类日期时间在数据库表中不能正常生成_第3张图片

OK,马到功成!


你可能感兴趣的:(web,后端Java,开发,SpringBoot开发,SpringDataJPA)