如何修复 "org.hibernate.boot.registry.selector.spi.StrategySelectionException: Unable to resolve name [org.hibernate.dialect.MySQL8Dialect]"?

How fix "org.hibernate.boot.registry.selector.spi.StrategySelectionException: Unable to resolve name [org.hibernate.dialect.MySQL8Dialect]"?

我在启动应用程序时收到异常:

Caused by: org.hibernate.boot.registry.selector.spi.StrategySelectionException: Unable to resolve name [org.hibernate.dialect.MySQL8Dialect] as strategy [org.hibernate.dialect.Dialect]
at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.selectStrategyImplementor(StrategySelectorImpl.java:126)

我正在使用 MySQL 8.0.

pom.xml:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.2.12.Final</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.15</version>
</dependency>

hibernate.cfg.xml:

    <property name="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost/botdb?serverTimezone=UTC</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">password</property>
    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.hbm2ddl.auto">update</property>
    <property name="hibernate.connection.useSSL">false</property>
    <property name="hibernate.enable_lazy_load_no_trans">true</property>

根据 pom.xml,您当前使用的是 Hibernate 5.2.12。从5.2 Javadocs可以看出没有MySQL8Dialect

MySQL8Dialect was introduced in Hibernate 5.3 (see Javadoc). Problem can be solved by updating to Hibernate to 5.3 or to 5.4.

如果无法更新 Hibernate,则必须创建自定义方言。这就像简单地将 MySQL8Dialect.java 从较新版本复制到您的代码库并从 persistence.xml 引用它一样简单。可能需要进行微小的修改。