1. 程式人生 > >菜鳥調錯(十二)——Spring Boo+JPA方式報錯:MappingException: Could not get constructor for org.hibernate.persister

菜鳥調錯(十二)——Spring Boo+JPA方式報錯:MappingException: Could not get constructor for org.hibernate.persister

文章目錄

錯誤資訊

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

結束

在解決這個問題的過程中,讓我有幾個感悟

  1. 走走彎路會收穫意外的美景
  2. 你學過的知識、做過的好事,會在你想不到的情況下幫你
  3. 程式設計是一件令人愉悅的事情

守住自己信的道。