当前位置: 首页 > 知识库问答 >
问题:

无法创建请求的服务[org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]

黄高爽
2023-03-14

我正在尝试将hibernate orm映射工具配置到我的java类,并使用PostgreSQL作为我的数据库,并将密码配置为“password”。当我尝试运行该应用程序时,我在控制台日志中遇到错误:无法创建请求的服务[org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]。我已经在老版本的hibernate上尝试过这个方法,它很管用。我现在使用的hibernate版本是5.1.0版。

以下是错误日志:

Mar 31, 2016 3:55:09 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.1.0.Final}
Mar 31, 2016 3:55:09 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Mar 31, 2016 3:55:09 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Mar 31, 2016 3:55:09 PM org.hibernate.boot.jaxb.internal.stax.LocalXmlResourceResolver resolveEntity
WARN: HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-configuration. Use namespace http://www.hibernate.org/dtd/hibernate-configuration instead.  Support for obsolete DTD/XSD namespaces may be removed at any time.
Mar 31, 2016 3:55:10 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
Mar 31, 2016 3:55:10 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
Mar 31, 2016 3:55:10 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [org.postgresql.Driver] at URL [jdbc:postgresql://localhost:5432/hibernatedb]
Mar 31, 2016 3:55:10 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=sa, password=****}
Mar 31, 2016 3:55:10 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
Mar 31, 2016 3:55:10 PM org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 1 (min=1)
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:244)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:208)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:189)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:51)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:217)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:189)
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.handleTypes(MetadataBuildingProcess.java:352)
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:111)
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
    at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418)
    at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:692)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)
    at org.javabrains.hibernate.HibernateTest.main(HibernateTest.java:18)
Caused by: org.hibernate.exception.JDBCConnectionException: Error calling Driver#connect
    at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator$1$1.convert(BasicConnectionCreator.java:105)
    at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.convertSqlException(BasicConnectionCreator.java:123)
    at org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:41)
    at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.createConnection(BasicConnectionCreator.java:58)
    at org.hibernate.engine.jdbc.connections.internal.PooledConnections.addConnections(PooledConnections.java:106)
    at org.hibernate.engine.jdbc.connections.internal.PooledConnections.<init>(PooledConnections.java:40)
    at org.hibernate.engine.jdbc.connections.internal.PooledConnections.<init>(PooledConnections.java:19)
    at org.hibernate.engine.jdbc.connections.internal.PooledConnections$Builder.build(PooledConnections.java:138)
    at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.buildPool(DriverManagerConnectionProviderImpl.java:110)
    at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:74)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:217)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:189)
    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.buildJdbcConnectionAccess(JdbcEnvironmentInitiator.java:145)
    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:66)
    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:88)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:234)
    ... 14 more
Caused by: org.postgresql.util.PSQLException: FATAL: password authentication failed for user "sa"
    at org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:433)
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:208)
    at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:66)
    at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:215)
    at org.postgresql.Driver.makeConnection(Driver.java:406)
    at org.postgresql.Driver.connect(Driver.java:274)
    at org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:38)
    ... 29 more

以下是我的hibernate.cfg.xml档案

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
        
        
<hibernate-configuration>
  <session-factory>
    <!-- Database connection settings -->
    <property name="connection.driver_class">org.postgresql.Driver</property>
    <property name="connection.url">jdbc:postgresql://localhost:5432/hibernatedb</property>
    <property name="connection.username">sa</property>
    <property name="connection.password">password</property>

    <!-- JDBC connection pool (use the built-in) -->
    <property name="connection.pool_size">1</property>

    <!-- SQL dialect -->
    <property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>

    <!-- Enable Hibernate's automatic session context management -->
    <property name="current_session_context_class">thread</property>

    <!-- Disable the second-level cache  -->
    <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">true</property>

    <!-- Drop and re-create the database schema on startup -->
    <property name="hbm2ddl.auto">create</property>
    
    <!-- Names the annotated entity class-->
    <mapping class="org.javabrains.dto.UserDetails"/>
    
  </session-factory>
</hibernate-configuration>

我的POJO课

package org.javabrains.dto;

import javax.persistence.Entity;

@Entity
public class UserDetails {
    
    private int userId;
    private String userName;
    
    public int getUserId() {
        return userId;
    }
    public void setUserId(int userId) {
        this.userId = userId;
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    
}

还有我的申请课:

package org.javabrains.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.javabrains.dto.UserDetails;

public class HibernateTest {

    public static void main(String[] args) {
        UserDetails user = new UserDetails();
    
        user.setUserId(1);
        user.setUserName("Tet");
        
        //Hibernate API to save this objects to DB
        //Session factory is created only ONCE
        SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
        Session session = sessionFactory.openSession();
        
        //create transaction
        session.beginTransaction();
        session.save(user);
        
        //end the transaction
        session.getTransaction().commit();
        
        //Closing the session
        session.close();
        
        
    }

}

下面是java hibernate结构的图像

共有3个答案

江光明
2023-03-14

您可能遇到了数据库连接问题,或者错过了连接到数据库(如数据库DIALECT)的任何Hibernate配置。

如果数据库服务器由于网络相关问题而断开连接,则会发生这种情况。

如果使用Spring Boot和hibernate连接到Oracle Db,请使用

hibernate.dialect=org.hibernate.dialect.HSQLDialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=update
hibernate.generate_statistics=true
entitymanager.packagesToScan: com
夏青青
2023-03-14

您不需要hibernate entitymanager xxx。jar,因为您使用的是Hibernate会话方法(而不是JPA)。您还需要关闭SessionFactory,并在出现错误时回滚事务。当然,问题不在于这些。

这由数据库返回

#
org.postgresql.util.PSQLException: FATAL: password authentication failed for user "sa"
#

看起来您提供了错误的用户名或(和)密码。

屠杰
2023-03-14

原因:发生此错误是因为Hibernate无法连接到数据库。
解决方案:
1.请确保您在配置文件中提到的服务器上存在一个数据库,例如。在这种情况下,“Hibernate”。
2. 请查看用于连接到数据库的用户名和密码是否正确。
3. 检查连接所需的相关 jar 是否映射到项目。

 类似资料:
  • 我正在使用XAMPP服务器和Eclipse IDE来运行一个hibernate程序。但我得到以下错误: **2017年4月21日12:46:38 PM org.Hibernate.version logVersion Info:hhh000412:Hibernate Core{5.2.8.final}2017年4月21日12:46:38 PM org.Hibernate.cfg.Environme

  • 我尝试在英国西部地区创建一个Azure服务容器。我经历了所有的步骤没有问题,但是一旦我点击创建后,我遇到了几分钟: 所提供的位置ukWest不适用于资源类型Microsoft。容器服务/容器服务。资源类型的可用区域列表是“日本东部、中部、东部2、日本西部、东部、南部、澳大利亚东部、澳大利亚东部、巴西南部、南部、西部、北部、西部、西部、北部、北部、北部、北部、东部”。 好吧,我意识到这是我的错误,于

  • 我已经安装了Tomcat7和8,但是如果我进入服务器选项卡并单击Create new Server,我会发现:它既不允许我插入服务器名称,也不允许我单击Finish。

  • 我知道这个问题以前被问过多次,但没有一个解决方案对我有帮助。 3个月前,我成功地做到了这一点,但现在我陷入了困境,没有改变任何让我困惑的事情,我真的不知道该怎么办! 1:基本信息:win 7 Rselenium standalone 3.13.0(试用过3.9.1,但不起作用,三个月前就开始工作了)chrome版本v64。0.3282.140(32位)chromedriver 2.37应兼容。(还

  • 这是我在Stackoverflow上的第一篇文章,如果我的文章格式化或提交不正确,请告诉我。我试图使用powershell连接到我的ServiceNow实例并发布服务目录请求。但是,我收到错误(400)错误请求: 到目前为止,我已经能够通过ServiceNow REST API Explorer使用URI: POST https://myinstance.service-now.com/API/s

  • 我正在使用Gradle构建一个java项目。当我运行任何任务(组装、测试)时。我随机得到一个错误: 以前有没有人有过同样的问题? Gradle V:3.5 java 8 我正在使用java插件。 谢谢