尝试创建 EntityManagerFactory 时线程“主”org.hibernate.service.spi.ServiceException 中的异常

Posted

技术标签:

【中文标题】尝试创建 EntityManagerFactory 时线程“主”org.hibernate.service.spi.ServiceException 中的异常【英文标题】:Exception in thread "main" org.hibernate.service.spi.ServiceException when trying to create EntityManagerFactory 【发布时间】:2020-09-23 19:09:37 【问题描述】:

我有使用 h2 数据库的工作应用程序。 db 没有连接问题,因为我可以从那里获取一些值但是当我尝试创建 EntityManagerFactory:EntityManagerFactory emf = Persistence.createEntityManagerFactory("EmployeeService"); 然后我在日志中看到:

2020-06-04 19:22:17.901  INFO 22496 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: EmployeeService]
2020-06-04 19:22:17.946  WARN 22496 --- [           main] o.h.e.j.c.i.ConnectionProviderInitiator  : HHH000181: No appropriate connection provider encountered, assuming application will be supplying connections
2020-06-04 19:22:17.946  WARN 22496 --- [           main] o.h.e.j.e.i.JdbcEnvironmentInitiator     : HHH000342: Could not obtain connection to query metadata : The application must supply JDBC connections
Exception in thread "main" 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:275)
Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
    at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImpl.java:100)

在 application.properties 我有:

#H2
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console/
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

#Flyway
flyway.user=sa
flyway.password=
flyway.url=jdbc:h2:mem:testdb
flyway.locations=filesystem:db/migration
spring.flyway.baseline-on-migrate = true

在 META-INF 中的 persistance.xml 中:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
    <persistence-unit name="EmployeeService">
        <class>com.example.demo.Employee.Employee</class>
        <properties>
            <property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:testdb"/>
        <property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
        <property name="javax.persistence.jdbc.user" value="sa"/>
        <property name="javax.persistence.jdbc.password" value=""/>
        </properties>
    </persistence-unit>
</persistence>

只有在尝试创建上述EntityManagerFactory 时才会出现问题。当我不尝试时,我可以转到 h2 web 控制台并查看 flyway 正确创建了 db,进行了插入,我可以进行选择/插入等等。

@编辑 添加 pom.xml

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.spingframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

【问题讨论】:

这能回答你的问题吗? Connection cannot be null when 'hibernate.dialect' not set @fjsventura,很遗憾,没有,我看到了它,它是关于会话工厂和休眠的,正如你所见,我的配置文件中有 connection.url 和 connection.driver。 我认为在使用 Spring 或 Spring Boot 时不需要persistance.xml,顺便说一句 您是否尝试在persistance.xml 中设置方言? 哦,我很笨,@chillworld,谢谢!您可以添加作为答案吗? 【参考方案1】:

如果你检查异常,它抱怨他在你的 persistence.xml 中没有方言。

请补充

<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>

【讨论】:

以上是关于尝试创建 EntityManagerFactory 时线程“主”org.hibernate.service.spi.ServiceException 中的异常的主要内容,如果未能解决你的问题,请参考以下文章

Spring:创建在类路径资源中定义的名称为“entityManagerFactory”的bean时出错

使用 MongoDB 休眠 OGM - 创建 EntityManagerFactory 时出现 ClassNotFoundException

在类路径中定义名称为“entityManagerFactory”的 bean 创建错误

SpringBoot JPA Hibernate:创建名为'entityManagerFactory'的bean时出错

在类路径资源中定义名称为“entityManagerFactory”的 bean 创建错误(调用 init 方法失败)

添加spring boot jpa依赖项时创建名称为“entityManagerFactory”的bean时出错