在尝试创建EntityManagerFactory时,出现线程"main"中的异常org.hibernate.service.spi.ServiceException。

24 浏览
0 Comments

在尝试创建EntityManagerFactory时,出现线程"main"中的异常org.hibernate.service.spi.ServiceException。

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

2020-06-04 19:22:17.901  INFO 22496 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: 处理PersistenceUnitInfo [name: EmployeeService]
2020-06-04 19:22:17.946  WARN 22496 --- [           main] o.h.e.j.c.i.ConnectionProviderInitiator  : HHH000181: 没有找到合适的连接提供程序,假设应用程序将提供连接
2020-06-04 19:22:17.946  WARN 22496 --- [           main] o.h.e.j.e.i.JdbcEnvironmentInitiator     : HHH000342: 无法获取连接以查询元数据:应用程序必须提供JDBC连接
Exception in thread "main" org.hibernate.service.spi.ServiceException: 无法创建请求的服务[org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:275)
Caused by: org.hibernate.HibernateException: 当'hibernate.dialect'未设置时,无法将DialectResolutionInfo访问为空
    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中:



    
        com.example.demo.Employee.Employee
        
            
        
        
        
        
    

问题只在尝试创建上述的EntityManagerFactory时出现。当我不尝试时,我可以进入h2 web控制台并看到flyway正确地创建了数据库,并进行了插入操作,我可以进行查询/插入等操作。

@Edit

添加了pom.xml


        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-data-jpa
        
        
            com.h2database
            h2
            runtime
        
        
            org.spingframework.boot
            spring-boot-starter-test
        
        
            org.flywaydb
            flyway-core
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
    

0
0 Comments

出现异常(Exception in thread "main" org.hibernate.service.spi.ServiceException when trying to create EntityManagerFactory)的原因是在persistence.xml中缺少hibernate.dialect属性的配置。

解决方法是在persistence.xml中添加以下配置:


这样就可以解决该异常。

0