休眠:PostgreSQL 驱动程序问题

Hibernate: PostgreSQL Driver issue

我知道已经有类似的问题,但那里的答案对我没有帮助。那么,您介意看看我的具体问题吗?

我对 Hibernate 还不是很熟悉,在尝试使用 Hibernate 4.3 和 PostgreSQL 为我的本地数据库创建测试数据时遇到了问题。

我有另一个项目,我以完全相同的方式执行此操作并且在那里工作,所以我进行了完全相同的设置但使用了另一个数据库,但现在在我当前的项目中我得到以下异常:

exception.DBException: Could not configure Hibernate!
    at dao.BenutzerDAO.<init>(BenutzerDAO.java:48)
    at export.ExportDBSchema.main(ExportDBSchema.java:16)
Caused by: org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [org.postgresql.Driver]
    at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:245)
    at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.loadDriverIfPossible(DriverManagerConnectionProviderImpl.java:200)
    at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.buildCreator(DriverManagerConnectionProviderImpl.java:156)
    at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:95)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:89)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:206)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:178)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:260)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:94)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:89)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:206)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:178)
    at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1885)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1843)
    at dao.BenutzerDAO.<init>(BenutzerDAO.java:45)
    ... 1 more
Caused by: java.lang.ClassNotFoundException: Could not load requested class : org.postgresql.Driver
    at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl$AggregatedClassLoader.findClass(ClassLoaderServiceImpl.java:230)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:340)
    at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:242)
    ... 15 more

我搜索了可能的解决方案,但 none 对我有用:

-)在 Manifest.mf 中指定 jar 的类路径 -> 无效 -) 将 postgresql-9.4.1208.jre6.jar 放在 WEB-INF 下的 lib 文件夹中 -> 没有用 -)在Configuration().configure()中指定hibernate.cfg.xml文件; -> 无效

我使用 Glassfish 4.1,org.postgres.Driver.class 已经存在,为什么找不到?

我的hibernate.cfg.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
    <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
    <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/Testdb</property>
    <property name="hibernate.connection.username">username</property>
    <property name="hibernate.hbm2ddl.auto">create-drop</property>
    <property name="hibernate.connection.password">password</property>
    <mapping class="entity.Benutzer"/>
  </session-factory>
</hibernate-configuration>

DAO中的方法class发生异常的地方:

try {
            if (sessionFactory == null) {
                Configuration conf = new Configuration().configure();

                StandardServiceRegistryBuilder builder
                        = new StandardServiceRegistryBuilder();
                builder.applySettings(conf.getProperties());

                sessionFactory = conf.buildSessionFactory(builder.build());
            }
        } catch (Throwable ex) {
            throw new DBException("Could not configure Hibernate!", ex);
        }

我将非常感谢您的每一个回答。

你很接近!

您需要将 postgresql-<version>.jar 直接放在 servlet 容器.
lib 文件夹中 例如,如果您正在使用 Apache Tomcat,只需将 jar 放在 <TOMCAT_ROOT>/lib 下,您应该一切就绪。

如果您使用的是 maven 或 gradle。

您可以将此添加到您的 pom.xml for maven

<!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>42.2.4</version>
</dependency>

这是 gradle

// https://mvnrepository.com/artifact/org.postgresql/postgresql
compile group: 'org.postgresql', name: 'postgresql', version: '42.2.4'