错误信息
018-10-04 15:58:02.540 ERROR 4572 --- [ main] o.s.boot.SpringApplication Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1699) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:573) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1089) ~[spring-context-5.0.9.RELEASE.jar:5.0.9.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:859) ~[spring-context-5.0.9.RELEASE.jar:5.0.9.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.0.9.RELEASE.jar:5.0.9.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:780) ~[spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:412) ~[spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:333) ~[spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1277) ~[spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1265) ~[spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]
at com.shuijing.boot.BootApplication.main(BootApplication.java:10) ~[classes/:na]
软件版本
- SpringBoot 2.0.5
- MySQL 8.0.12
- JDK 11
- IDEA 2018.2
- Windows 10
错误背景
使用@Entity
注解根据Java对象生成表结构。
前提
- getter and setter 和构造方法均准确无误
- drivername 为
com.mysql.cj.jdbc.Driver
- mysql-connector-java 依赖显式声明为
8.0.12
版本 - 使用SpringBoot,不存在配置文件跟实体属性不一致的问题
推理
基于以上情况,开始怀疑是不是MySQL这里出了问题,版本太新不稳定(此时8.0.12是最新版)?解压版配置的不对,相比安装版少了服务?
于是下载了5.6,相应的配置也改成了5.6的版本,结果错误依然存在。为了确保MySQL的配置没问题,前前后后练习了三四遍,倒是把MySQL的安装配置摸得门儿清。一不留神就写了一篇小教程,请戳 Windows 10安装MySQL 8.0.12 解压版 。
后来凭借我夜观天象,啊呸!凭借我多年撸代码的经验,终于定位到了真正的原因,因为有过使用Hibernate的经验(为了证明我真的有使用Hibernate的经验,把我N年前写的SSH专栏奉上),让我想到要用到Hibernate的JPA特性,那Hibernate依赖的核心Jar包肯定不能少呀!于是在我的尝试之下,发现需要在pom文件中加入对 javassist
的依赖。
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.23.1-GA</version>
</dependency>
可能遇到的一些问题
- SSL安全连接问题
- 时区问题
The server time zone value 'Öйú±ê׼ʱ¼ä'
以上问题只需将数据库驱动换成对应版本,并加上相应配置即可解决。
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/boot?serverTimezone=UTC&allowPublicKeyRetrieval=true&useSSL=false
结束
在解决这个问题的过程中,让我有几个感悟
- 走走弯路会收获意外的美景
- 你学过的知识、做过的好事,会在你想不到的情况下帮你
- 编程是一件令人愉悦的事情
守住自己信的道。