2016-07-09 19 views
0

私はHibernateでまだ経験が豊富で、havaはHibernate 4.3とPostgreSQLでローカルデータベースのテストデータを作成しようとしているときに問題があります。私はこの全く同じ方法を行なったし、そこにどこHibernateデータベースドライバクラスが見つかりません

は、私はそれが別のプロジェクトを働いていたので、私はまったく同じセットアップをしましたが、別のデータベースを持つが、今、私の現在のプロジェクトで、私は次の例外を取得:

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 

私は可能な解決策を探し、それらのどれも私のために働いていない:

- )でManifest.mfにジャーにクラスパスを指定します。 - >
を動作しませんでした - )のpostgresql-9.4.1208.jre6.jarを置きますWEB-INF下のlibフォルダにあります - >動作しませんでした
- )hibernaを指定してくださいConfiguration()のte.cfg.xmlファイル。 - >うまくいきませんでした

私はGlassfish 4.1を使用していますが、org.postgres.Driver.classが存在するため、なぜ見つからないのですか?

私はhibernate.cfg.xml:例外が発生しBenutzerDAO.javaで

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

方法:

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

私はすべての答えのために非常に感謝するでしょう。

+0

の可能性のある重複:重複した質問を投稿[休止状態PostgreSQLのドライバの問題](http://stackoverflow.com/questions/38410850/hibernate-postgresql-driver-issue) –

+0

ストップ。ありがとうございました。 –

答えて

0
try 
{ 
    Configuration cfg =new Configuration(); 
    cfg.configure("hibernate.cfg.xml"); 

    SessionFactory sf=cfg.buildSessionFactory(); 
    Session sess=sf.openSession(); 
    ---------------------------------- 
your code 
------------------------------ 

Transaction tx=sess.beginTransaction(); 
sess.save(e); 

tx.commit(); 

sess.flush(); 
sess.close(); 
sf.close(); 


} 
catch(Exception e) 
{ 
System.out.println("e="+e.getMessage()); 
} 
+0

私は完全に返信することを忘れて申し訳ありません:/あなたの答えは非常にありがとうが、問題はすでに解決されています:) – boehmP

+0

クール............ –

関連する問題