无法在 Hibernate JPA 中创建请求的服务 [org.hibernate.engine.jdbc.env.sp



我是JPA,Hibernate和Maven的新手,我正在使用这个视频教程创建一个项目。我相信我在Hibernate上遇到了一些问题,但我在这个网站上看到的其他答案似乎并不能解决我的问题。我正在使用Hibernate 5.4.1.Final和mysql 8.0.20。下面是部分堆栈跟踪(full相当长(。

WARN: HHH000342: Could not obtain connection to query metadata : null
Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: 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)
...
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:79)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
at com.sommaven.JEETut3.TestSystem.<clinit>(TestSystem.java:15)
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)
at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:54)
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:137)
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:101)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:263)
...

这是我的坚持.xml

<?xml version="1.0" encoding="UTF-8"?>

<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

<!-- Define a name used to get an entity manager. Define that you will 
complete transactions with the DB  -->
<persistence-unit name="JEETut3" transaction-type="RESOURCE_LOCAL">

<!-- Define the class for Hibernate which implements JPA -->
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<!-- Define the object that should be persisted in the database -->
<class>com.newthinktank.JEETut3.Customer</class>
<properties>
<!-- Driver for DB database -->
<property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver" />
<!-- URL for DB -->
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/test4" />
<!-- Username -->
<property name="javax.persistence.jdbc.user" value="dbadmin" />
<!-- Password -->
<property name="javax.persistence.jdbc.password" value="turtledove" />
</properties>
</persistence-unit>
</persistence>

这是我的绒球.xml

<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>

<!-- 1. New -> Maven Project (Quick Start)
Group Id : com.newthinktank
Artifact ID : JEETut3
Update the dependencies here
Maven is project management software that handles builds, dependencies, documentation and more
-->

<groupId>com.newthinktank</groupId>
<artifactId>JEETut3</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>JEETut3</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.1.Final</version>
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.4.1.Final</version>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

我看到没有设置hibernate.dialect,我需要手动将其设置为mysql吗?

您看到此异常,因为您没有在persistence.xml中提供任何方言,并将其留给Hibernate在启动时自动解析。Hibernate可以确定要自动使用的正确方言,但为了做到这一点,它需要与数据库的实时连接。我认为由于连接问题,方言在启动时无法为您解决,因此请尝试将以下内容添加到您的persistence.xml

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

您还可以检查以验证您尝试连接到的数据库服务器是否已启动并正在运行,或者驱动程序版本是否兼容。还要仔细检查url,密码等,以验证您是否遗漏了需要指定的任何特定端口等。

最新更新