JPA Hibernate。如何找到方言?

3

我正在为一个学校项目开发rest API,我的问题是如何找到正确的方言版本?我对这个话题不是很了解,但我知道hibernate包含在JPA依赖项中吗? 如果不是,我需要单独安装它吗?我可以看到编译器正在自动填充我的属性选择,所以我想hibernate是与依赖项一起提供的。

我正在使用以下依赖项开发Spring Boot项目: Spring Web Spring JPA MySQL Driver

我尝试了这个hibernate属性,但是我得到了一个错误。

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect

Caused by: org.hibernate.boot.registry.selector.spi.StrategySelectionException: Unable to resolve name [org.hibernate.dialect.MySQL5InnoDBDialect] as strategy [org.hibernate.dialect.Dialect]
    at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.selectStrategyImplementor(StrategySelectorImpl.java:155) ~[hibernate-core-6.1.5.Final.jar:6.1.5.Final]
    at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.resolveStrategy(StrategySelectorImpl.java:237) ~[hibernate-core-6.1.5.Final.jar:6.1.5.Final]
    at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.resolveStrategy(StrategySelectorImpl.java:190) ~[hibernate-core-6.1.5.Final.jar:6.1.5.Final]
    at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.constructDialect(DialectFactoryImpl.java:96) ~[hibernate-core-6.1.5.Final.jar:6.1.5.Final]
    at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:59) ~[hibernate-core-6.1.5.Final.jar:6.1.5.Final]
    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:244) ~[hibernate-core-6.1.5.Final.jar:6.1.5.Final]
    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:36) ~[hibernate-core-6.1.5.Final.jar:6.1.5.Final]
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:119) ~[hibernate-core-6.1.5.Final.jar:6.1.5.Final]
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:255) ~[hibernate-core-6.1.5.Final.jar:6.1.5.Final]
    ... 31 common frames omitted
Caused by: org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [org.hibernate.dialect.MySQL5InnoDBDialect]
    at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:123) ~[hibernate-core-6.1.5.Final.jar:6.1.5.Final]
    at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.selectStrategyImplementor(StrategySelectorImpl.java:151) ~[hibernate-core-6.1.5.Final.jar:6.1.5.Final]
    ... 39 common frames omitted
Caused by: java.lang.ClassNotFoundException: Could not load requested class : org.hibernate.dialect.MySQL5InnoDBDialect
    at org.hibernate.boot.registry.classloading.internal.AggregatedClassLoader.findClass(AggregatedClassLoader.java:210) ~[hibernate-core-6.1.5.Final.jar:6.1.5.Final]
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:588) ~[na:na]
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521) ~[na:na]
    at java.base/java.lang.Class.forName0(Native Method) ~[na:na]
    at java.base/java.lang.Class.forName(Class.java:495) ~[na:na]
    at java.base/java.lang.Class.forName(Class.java:474) ~[na:na]
    at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:120) ~[hibernate-core-6.1.5.Final.jar:6.1.5.Final]
    ... 40 common frames omitted


This my pom.xml file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>SecureSoftwareDevProject</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>SecureSoftwareDevProject</name>
    <description>SecureSoftwareDevProject</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

我正在运行 MySQL 服务器 8.0.31


你的项目的pom.xml文件中包含哪个JDBC JAR文件,我们需要知道这一点。另外,请提供底层数据库的供应商和版本信息。 - Tim Biegeleisen
编辑了回答您的问题,希望这就是您所询问的内容? - Pseud0nym
我认为我找到了一个可能的解决方法。完全删除命令spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect会自动将方言标记为:org.hibernate.dialect.MySQLDialect。虽然我不知道这是否会在未来造成问题。 - Pseud0nym
3个回答

3

Spring Boot 3使用Hibernate 6。Hibernate 6改变了方言的工作方式,您需要使用org.hibernate.dialect.MySQLDialect(它会根据您实际的服务器版本进行配置)。在Hibernate 6中,名称中包含InnoDB的特定版本方言已被删除,但仍有一些特定版本的方言(例如org.hibernate.dialect.MySQL8Dialect),但这些方言已在Hibernate 6发布时被弃用。


Hibernate 6会自动找到正确的方言,无需手动指定方言。请参阅Hibernate文档 - undefined
@Raffael 不一定需要,但你仍然可以指定它,如果你从之前的版本迁移,很可能会明确列出它。 - undefined
@Raffael 我已经更新了我的回答,谢谢。 - undefined

1
如果您正在使用MySQL服务器8.0.31,则在application.properties文件中添加以下内容应该可以正常工作:
spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.MySQL8Dialect

1
Hibernate 6 已经弃用了 org.hibernate.dialect.MySQL8Dialect。推荐使用 org.hibernate.dialect.MySQLDialect - Mark Rotteveel

0
您可以查看文档,其中包含以下内容:

MySQLDialect -> 适用于 MySQL(5.x 之前版本)的 SQL 方言。

Hibernate 方言


这不适用于使用Spring Boot 3的Hibernate 6。在Hibernate 6中,MySQLDialect是所有支持的MySQL版本的方言。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接