亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無法為 hibernate 項目創建請求的服務

無法為 hibernate 項目創建請求的服務

慕妹3242003 2023-09-06 16:54:14
當我嘗試使用 Eclipse 中的 PostgreSQL 使用 Jdbc 配置來運行 Hibernate 示例項目時,出現錯誤“線程“main”中的異常 org.hibernate.service.spi.ServiceException: 無法創建請求的服務 [org.hibernate.engine. jdbc.env.spi.JdbcEnvironment] “。即使在搜索類似類型的問題后,我也無法解決此錯誤。這是為了使用Hibernate 5.2.17、PostgreSQL 11和Jdk 1.8運行新的 hibernate 項目。我嘗試使用 Hibernate v4.2.0 運行,但它給出了 jdbc 錯誤“ClassLoadingException”。我的 hibernate.cfg.xml 文件:<!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD//EN""http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration>    <session-factory>        <property name="hibernate.connection.driver_class">org.postgres.Driver</property>        <property name="hibernate.connection.url">jdbc:postgres://localhost:5432/hibernatedb</property>        <property name="hibernate.connection.username">postgres</property>        <property name="hibernate.connection.password">password</property>        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>        <property name="connection.pool_size">1</property>        <!-- Disable the second-level cache -->        <property name="cache.provider_class">org.hibernate.NoCacheProvider</property>        <!-- Echo all executed SQL to stdout -->        <property name="show_sql">1</property>        <!-- Drop and re-create the database schema on startup -->        <property name="hibernate.hbm2ddl.auto">create</property>        <!-- Names the annotated entity class -->        <mapping class="home.practice.hibernate.dto.UserDetails"/>    </session-factory></hibernate-configuration>我的模型類 ieUserDetails.javapackage home.practice.hibernate.dto;import javax.persistence.Entity;import javax.persistence.Id;@Entitypublic class UserDetails {    @Id    private int userId;    private String userName;    public int getUserId() {        return userId;    }
查看完整描述

1 回答

?
躍然一笑

TA貢獻1826條經驗 獲得超6個贊

當您看到帶有如下堆棧跟蹤的異常時:

Caused by: java.lang.ClassNotFoundException: Could not load requested class : org.postgres.Driver
    [...]
    at java.lang.Class.forName(Unknown Source)
    [...]

類路徑中很可能沒有類加載器由于 Class.forName 調用而嘗試加載的類。

當使用 Class.forName 動態加載類時,該類僅在運行時需要,而不是在編譯時需要,并且當不可用時,它會生成此類錯誤。

您的 hibernate.cfg.xml 中有一個拼寫錯誤:

<property name="hibernate.connection.driver_class">org.postgres.Driver</property>

driver_classClass.forName這是傳遞給堆棧跟蹤中的調用的類;postgres jdbc 驅動程序的正確類名org.postgresql.Driver不是org.postgres.Driver,所以

<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>


查看完整回答
反對 回復 2023-09-06
  • 1 回答
  • 0 關注
  • 115 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號