Spring Boot集成MySQL8.0.32启动报错:Hibernate方言类无法加载
MySQL8.0.32连接Spring Boot时Hibernate方言加载失败的解决方案
问题描述
在Eclipse中将MySQL8.0.32连接到Spring Boot项目,运行主类时出现报错,错误指向application.properties中的方言配置项,无法加载指定的Hibernate方言类,最终导致entityManagerFactory Bean创建失败。
配置文件信息
application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/emp?useSSL=false spring.datasource.username=root spring.datasource.password=root #hibernate properties spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBdialect #create,create-drop spring.hibernate.ddl-auto=update
主类代码
StringProject1Application.java
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class StringProject1Application { public static void main(String[] args) { SpringApplication.run(StringProject1Application.class, args); } }
报错信息
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 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.6.Final.jar:6.1.6.Final] at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.selectStrategyImplementor(StrategySelectorImpl.java:151) ~[hibernate-core-6.1.6.Final.jar:6.1.6.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.6.Final.jar:6.1.6.Final] at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:587) ~[na:na] at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520) ~[na:na] at java.base/java.lang.Class.forName0(Native Method) ~[na:na] at java.base/java.lang.Class.forName(Class.java:467) ~[na:na] at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:120) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final] ... 40 common frames omitted org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1751) ~[spring-beans-6.0.4.jar:6.0.4] at org.springframework.beans.factory. Caused by: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:267) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final] at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:230) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final] at org.hibernate.service.
解决步骤
修正方言类名与适配MySQL8.0
原配置中的org.hibernate.dialect.MySQL5InnoDBdialect存在两个问题:一是类名拼写错误(dialect首字母需大写为Dialect,正确类名为org.hibernate.dialect.MySQL5InnoDBDialect);二是该方言适配MySQL5.x版本,与你使用的MySQL8.0不匹配。建议替换为MySQL8.0对应的方言:spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect修正ddl-auto配置项前缀
Spring Boot中JPA的DDL配置项前缀应为spring.jpa.hibernate,而非spring.hibernate,修改后:spring.jpa.hibernate.ddl-auto=update可选:添加时区配置
MySQL8.0默认要求时区配置,可在数据源URL中添加时区参数避免潜在报错:spring.datasource.url=jdbc:mysql://localhost:3306/emp?useSSL=false&serverTimezone=Asia/Shanghai
修改后的完整application.properties示例:
spring.datasource.url=jdbc:mysql://localhost:3306/emp?useSSL=false&serverTimezone=Asia/Shanghai spring.datasource.username=root spring.datasource.password=root #hibernate properties spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect #create,create-drop spring.jpa.hibernate.ddl-auto=update
内容的提问来源于stack exchange,提问作者Sireesha jinkala
相关产品推荐
相关产品推荐

