在SpringBoot 3.2.1中使用JPA报错

发布时间:2024年01月19日

在SpringBoot 3.2.1中使用JPA报错

最近在对接大模型,需要把机器人信息持久化到数据库,使用最简单的方式就是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>

参考文档

参考文档
更多内容

文章来源:https://blog.csdn.net/qq_43907505/article/details/135686987
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。