2014-10-09 24 views
1

我遇到了一些獨特的問題。有關使用休眠的Oracle JDBC的問題...有時候

我能夠在運行JUnit測試時成功連接和管理實體,但是一旦啓動我的實際應用程序,就會收到「未找到指定的JDBC驅動程序oracle.jdbc.OracleDriver類」。

讓我感到困惑的是它在那裏。它運行我的JUnit測試時,它工作

感謝您的任何見解!

的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 name="db"> 
     <property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property> 
     <property name="hibernate.connection.url">jdbc:oracle:thin:@host:port/db</property> 
     <property name="hibernate.connection.username">username</property> 
     <property name="hibernate.connection.password">password</property> 
     <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property> 
     <property name="hibernate.default_schema">db</property> 
     <property name="show_sql">true</property> 
     <mapping resource="org/entity/RunResultEntity.hbm.xml"/> 
     <mapping resource="org/entity/TransactionResultEntity.hbm.xml"/> 
     <mapping resource="org/entity/FailureResultEntity.hbm.xml"/> 
    </session-factory> 
</hibernate-configuration> 

HibernateUtil.java

package org.util; 

import org.hibernate.SessionFactory; 
import org.hibernate.cfg.Configuration; 
import org.hibernate.service.*; 

public class HibernateUtil { 

    private static SessionFactory sessionFactory = buildSessionFactory(); 

    private static SessionFactory buildSessionFactory() { 
     try { 
      // Create the SessionFactory from hibernate.cfg.xml 
      Configuration configuration = new Configuration(); 
      configuration.configure(); 
      ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(
       configuration.getProperties() 
      ).buildServiceRegistry(); 
      sessionFactory = configuration.buildSessionFactory(serviceRegistry); 
      return sessionFactory; 
     } catch (Throwable ex) { 
      // Exception thrown here! 
      System.err.println("Initial SessionFactory creation failed." + ex); 
      throw new ExceptionInInitializerError(ex); 
     } 
    } 

    public static SessionFactory getSessionFactory() { 
     return sessionFactory; 
    } 

    public static void shutdown() { 
     // Close caches and connection pools 
     getSessionFactory().close(); 
    } 

} 

的pom.xml(依賴添加到本地資源庫)

<dependency> 
    <groupId>com.oracle</groupId> 
    <artifactId>ojdbc6</artifactId> 
    <version>11.2.0.3.0</version> 
    <scope>provided</scope> 
</dependency> 

日誌

Oct 09, 2014 3:02:58 PM org.hibernate.annotations.common.Version <clinit> 
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final} 
Oct 09, 2014 3:02:58 PM org.hibernate.Version logVersion 
INFO: HHH000412: Hibernate Core {4.0.1.Final} 
Oct 09, 2014 3:02:58 PM org.hibernate.cfg.Environment <clinit> 
INFO: HHH000206: hibernate.properties not found 
Oct 09, 2014 3:02:58 PM org.hibernate.cfg.Environment buildBytecodeProvider 
INFO: HHH000021: Bytecode provider name : javassist 
Oct 09, 2014 3:02:58 PM org.hibernate.cfg.Configuration configure 
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml 
Oct 09, 2014 3:02:58 PM org.hibernate.cfg.Configuration getConfigurationInputStream 
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml 
Oct 09, 2014 3:02:58 PM org.hibernate.cfg.Configuration addResource 
INFO: HHH000221: Reading mappings from resource: org/entity/RunResultEntity.hbm.xml 
Oct 09, 2014 3:02:59 PM org.hibernate.cfg.Configuration addResource 
INFO: HHH000221: Reading mappings from resource: org/entity/TransactionResultEntity.hbm.xml 
Oct 09, 2014 3:02:59 PM org.hibernate.cfg.Configuration addResource 
INFO: HHH000221: Reading mappings from resource: org/entity/FailureResultEntity.hbm.xml 
Oct 09, 2014 3:02:59 PM org.hibernate.cfg.Configuration doConfigure 
INFO: HHH000041: Configured SessionFactory: db 
Oct 09, 2014 3:02:59 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure 
INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!) 
Initial SessionFactory creation failed.org.hibernate.HibernateException: Specified JDBC Driver oracle.jdbc.OracleDriver class not found 
Exception in thread "main" java.lang.ExceptionInInitializerError 

回答

1

我發現我的問題。低於我的pom.xml我有這個小片段

<dependency> 
    <groupId>org.hibernate.common</groupId> 
    <artifactId>hibernate-commons-annotations</artifactId> 
    <version>4.0.1.Final</version> 
    <classifier>tests</classifier> 
</dependency> 

分類器只能訪問我的測試套件。刪除分類器解決了問題。

<dependency> 
    <groupId>org.hibernate.common</groupId> 
    <artifactId>hibernate-commons-annotations</artifactId> 
    <version>4.0.1.Final</version> 
</dependency>