我在运行Spring Boot项目时一直遇到相同的问题,尝试使用Spring Data JPA连接到MySQL。它说:“初始化方法失败;嵌套的异常是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

最佳答案

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

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

关于java - 创建在类路径资源中定义的名称为'entityManagerFactory'的bean时出错-调用init方法失败,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47206958/

10-10 21:39