【问题标题】:Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] in Hibernate JPA无法在 Hibernate JPA 中创建请求的服务 [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
【发布时间】:2020-10-13 01:52:29
【问题描述】:

我是 JPA、Hibernate 和 Maven 的新手,我正在使用 this video tutorial 创建一个项目。我相信我在使用 Hibernate 时遇到了一些问题,但是我在这个网站上看到的其他答案似乎并没有解决我的问题。我正在使用 Hibernate 5.4.1.Final 和 mysql 8.0.20。下面是部分堆栈跟踪(完整的很长)。

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

这是我的 persistence.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>

这是我的 pom.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?

【问题讨论】:

标签: java mysql hibernate maven jpa


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-20
    • 2021-12-22
    • 2021-02-23
    • 2017-03-27
    • 1970-01-01
    • 2017-12-30
    • 1970-01-01
    • 2017-09-18
    相关资源
    最近更新 更多