在类路径资源中定义名称为'entityManagerFactory'的bean时出错 - init方法的调用失败

时间:2017-11-09 16:29:28

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

我继续遇到运行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

1 个答案:

答案 0 :(得分:0)

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

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