【问题标题】:Error creating bean with name 'entityManagerFactory' defined in class path resource - invocation of init method failed在类路径资源中定义名称为“entityManagerFactory”的 bean 创建错误 - 调用 init 方法失败
【发布时间】:2017-11-09 16:29:28
【问题描述】:

我在运行我的 Spring Boot 项目时不断遇到同样的问题,尝试使用 Spring Data JPA 连接到 MySQL。它说“调用 init 方法失败;嵌套异常是 org.hibernate.service.spi.ServiceException:无法创建请求的服务 [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]”。不确定那是什么意思。尝试在这里查看其他已回答的问题,但无法弄清楚。有什么想法吗?

错误是:

org.springframework.beans.factory.BeanCreationException: Error creating bean 
with name 'entityManagerFactory' defined in class path 
resource[org/springframework/boot/autoconfigure/orm/jpa/
HibernateJpaAutoConfigura ion.class]: Invocation of init method failed; nested 
exception is 
org.hibernate.service.spi.ServiceException: Unable to create requested service 
[org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]

说原因:

Caused by: org.hibernate.service.spi.ServiceException: Unable to create 
requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]

Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo 
cannot be null when 'hibernate.dialect' not set

我的 pom.xml 文件是:

 <?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 
    http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

<name></name>
<description>project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.4.RELEASE</version>
    <relativePath/>
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</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>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
    </dependency>
</dependencies>

还有,我的 application.properties:

spring.datasource.url=jdbc:mysql://localhost:3306/<<projectname>>
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update

spring.mvc.view.prefix=/WEB-INF/
spring.mvc.view.suffix=.jsp

【问题讨论】:

标签: java spring hibernate spring-boot spring-data-jpa


【解决方案1】:

如果您仔细查看日志,它会显示:

Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo 
cannot be null when 'hibernate.dialect' not set

因此,您需要在application.properties 文件中再添加一行来设置 MySQL 方言:

spring.jpa.database-platform = org.hibernate.dialect.MySQL5Dialect

【讨论】:

  • 能否提供完整的堆栈跟踪
猜你喜欢
  • 2020-03-23
  • 2017-02-24
  • 2019-11-19
  • 2015-10-16
  • 2014-03-09
  • 2016-02-17
  • 1970-01-01
相关资源
最近更新 更多