Hibernate:PostgreSQL驱动程序问题 [英] Hibernate: PostgreSQL Driver issue

查看:152
本文介绍了Hibernate:PostgreSQL驱动程序问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道已经有类似的问题了,但是那里的答案没有帮助我。所以,请介意看看我的具体问题。



我对Hibernate还没有很多经验,而且在尝试为我的本地创建测试数据时遇到问题数据库与Hibernate 4.3和PostgreSQL。



我有另一个项目,我做了完全相同的方式,它的工作,所以我做了完全相同的设置,但与另一个数据库,但现在在我当前的项目中,我得到以下异常:

  exception.DBException:无法配置Hibernate! 
at dao.BenutzerDAO。< init>(BenutzerDAO.java:48)
at export.ExportDBSchema.main(ExportDBSchema.java:16)
Caused by:org.hibernate.boot。 registry.classloading.spi.ClassLoadingException:无法加载类[org.postgresql.Driver]
在org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:245)
在org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.loadDriverIfPossible(DriverManagerConnectionProviderImpl.java:200)
在org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.buildCreator(DriverManagerConnectionProviderImpl.java:156)在org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:95)

在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)$ b在org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:260)
$ b在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)
在org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:178)
在org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1885)
在org.hibernate.cfg .Configuration.buildSessionFactory(Configuration.java:1843)
at dao.BenutzerDAO。< init>(BenutzerDAO.java:45)
...另外1个
引用者:java.lang .ClassNotFoundException:无法加载请求的类:org.postgresql.Driver
。在org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl $ AggregatedClassLoader.findClass(ClassLoaderServiceImpl.java:230)
是java。 lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native方法)
在java.lang.Class.forName(Class.java:340)
在org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:242)
... 15 more

我搜索了可能的解决方案,但没有一个为我工作:



- )在Manifest.mf中为jar指定Classpath - >没有工作
- )将libg文件夹中的postgresql-9.4.1208.jre6.jar放在WEB-INF - >没有工作
- )在Configuration()中指定hibernate.cfg.xml文件。 - >没有工作



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



我的hibernate.cfg.xml:

 <?xml version =1.0encoding = UTF-8?> 
<!DOCTYPE hibernate-configuration PUBLIC - // Hibernate / Hibernate Configuration DTD 3.0 // ENhttp://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类中的方法:

  try {
if(sessionFactory == null){
配置conf = new Configuration()。

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

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

我非常感谢每一个答案。

$ b $



您需要将您的 postgresql- servlet容器 lib 文件夹中直接打开 例如,如果您使用 Apache Tomcat ,只需将 jar 放在< TOMCAT_ROOT> / lib 你应该全部设置。


I know that there are similar questions already, but the answers there didn't help me. So please would you mind to take a look at my particular question?

I am not very experienced with Hibernate yet and hava a problem when trying to create test data for my local database with Hibernate 4.3 and PostgreSQL.

I had another project where I did this exactly the same way and there it worked, so I did exactly the same setup but with another database, but now in my current project I get the following exception:

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

I searched for possible solutions, but none of them worked for me:

-)Specify Classpath to jar in Manifest.mf -> Did not work -)Place the postgresql-9.4.1208.jre6.jar in lib folder under WEB-INF -> Did not work -)Specify hibernate.cfg.xml file in Configuration().configure(); -> Did not work

I use Glassfish 4.1 and the org.postgres.Driver.class is existing, so why is it not found?

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

Method in DAO class where the exception occurs:

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);
        }

I would be very thankful for every answer.

解决方案

You were close!

You need to place your postgresql-<version>.jar directly in the lib folder of your servlet container.
For instance, if you're working with Apache Tomcat, simply drop your jar under <TOMCAT_ROOT>/lib and you should be all set.

这篇关于Hibernate:PostgreSQL驱动程序问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆