最近在对接大模型,需要把机器人信息持久化到数据库,使用最简单的方式就是JPA了,所以我在pom.xml文件中引入了下面的依赖。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
spring-boot-starter-data-jpa
中是包括了hibernate-core
依赖的,版本是Hibernate 6.4
。由于该版本中移除了MySQL57Dialect
,所以你启动项目时应该会看到下面的报错信息。
[ERROR] 2023-12-22 10:44:44 SpringApplication - Application run failed
java.lang.ClassNotFoundException: org.hibernate.dialect.MySQL57Dialect
at java.base/java.net.URLClassLoader.findClass(Unknown Source) ~[na:na]
at java.base/java.lang.ClassLoader.loadClass(Unknown Source) ~[na:na]
at org.springframework.boot.loader.net.protocol.jar.JarUrlClassLoader.loadClass(JarUrlClassLoader.java:104) ~[XXX]
at org.springframework.boot.loader.launch.LaunchedClassLoader.loadClass(LaunchedClassLoader.java:91) ~[XXX]
at java.base/java.lang.ClassLoader.loadClass(Unknown Source) ~[na:na]
... 28 common frames omitted
Wrapped by: java.lang.NoClassDefFoundError: org/hibernate/dialect/MySQL57Dialect
at org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter.determineDatabaseDialectClass(HibernateJpaVendorAdapter.java:203) ~[spring-orm-6.1.2.jar!/:6.1.2]
at org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter.buildJpaPropertyMap(HibernateJpaVendorAdapter.java:148) ~[spring-orm-6.1.2.jar!/:6.1.2]
at org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter.getJpaPropertyMap(HibernateJpaVendorAdapter.java:132) ~[spring-orm-6.1.2.jar!/:6.1.2]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:365) ~[spring-orm-6.1.2.jar!/:6.1.2]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:352) ~[spring-orm-6.1.2.jar!/:6.1.2]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) ~[spring-boot-3.2.1.jar!/:3.2.1] [11 skipped]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:762) ~[spring-boot-3.2.1.jar!/:3.2.1]
... 21 common frames omitted
Wrapped by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [XXX/MySqlConfig.class]: org/hibernate/dialect/MySQL57Dialect
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) ~[spring-boot-3.2.1.jar!/:3.2.1] [10 skipped]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:762) ~[spring-boot-3.2.1.jar!/:3.2.1]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:464) ~[spring-boot-3.2.1.jar!/:3.2.1]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:334) ~[spring-boot-3.2.1.jar!/:3.2.1]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1358) ~[spring-boot-3.2.1.jar!/:3.2.1]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1347) ~[spring-boot-3.2.1.jar!/:3.2.1]
at XXX.Main.main(Main.java:53) ~[!/:na]
at org.springframework.boot.loader.launch.Launcher.launch(Launcher.java:91) ~[XXX] [2 skipped]
at org.springframework.boot.loader.launch.Launcher.launch(Launcher.java:53) ~[XXX]
at org.springframework.boot.loader.launch.JarLauncher.main(JarLauncher.java:58) ~[XXX]
这是hibernate-core
版本太高导致的,我们修改一下pom.xml
文件。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<exclusions>
<exclusion>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-core</artifactId>
</exclusion>
</exclusions>
</dependency>
然后引入低版本的hibernate-core
。
<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.2.7.Final</version>
</dependency>