2013-07-10 129 views
2

我想在Oracle 11g中使用Hibernate 4.2。 以下是我的hibernate.cfg.xml文件:Oracle11g + hibernate 4.2 buildSessionFactory拋出Nullpointer異常

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE hibernate-configuration PUBLIC 
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 
<hibernate-configuration> 
<session-factory name="sessionFactory"> 
<property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property> 
<property name="hibernate.connection.password">[email protected]</property> 
<property  name="hibernate.connection.url">  
jdbc:oracle:thin:ourhost:1521:oursid</property> 
<property name="hibernate.connection.username">OURUSER</property> 
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property> 

<mapping resource="resources/mapping/User.hbm.xml"/> 
</session-factory> 
</hibernate-configuration> 

而我的Util類來獲得會話工廠如下:

public class HibernateUtil { 

private SessionFactory sessionFactory; 

public HibernateUtil() { 
    sessionFactory = createSessionFactory(); 
} 

private static SessionFactory createSessionFactory() { 
     SessionFactory factory = null; 
    try { 
     Configuration configuration = new Configuration(); 
     configuration.configure("resources/hibernate.cfg.xml"); 
     factory = configuration.buildSessionFactory();   
    } catch (Throwable ex) { 

     System.err.println("Initial SessionFactory creation failed."); 
     ex.printStackTrace(); 
     throw new ExceptionInInitializerError(ex); 

    } 
    return factory; 

} 

public SessionFactory getSessionFactory(){ 
    return this.sessionFactory; 
} 
} 

這工作得很好,如果我使用MySQL數據庫和相關的休​​眠。 cfg.xml中。如果我像上面提到的那樣爲Oracle使用相同的東西,那麼它根本就沒有連接。我得到以下異常:

Initial SessionFactory creation failed. 
    java.lang.NullPointerException 
at  org.hibernate.engine.jdbc.internal. 
    JdbcServicesImpl.configure(JdbcServicesImpl.java:207) 
at org.hibernate.service.internal. 
    StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75) 
at org.hibernate.service.internal. 
    AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:159) 
at org.hibernate.service.internal. 
    AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131) 
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:78) 
at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2283) 
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2279) 
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1748) 
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1788) 
at 
com.bosch.asaw.utilities.HibernateUtil.createSessionFactory(HibernateUtil.java:25) 
at com.bosch.asaw.utilities.HibernateUtil.<init>(HibernateUtil.java:17) 
at com.bosch.asaw.dao.testhibernate.main(testhibernate.java:19) 
    Exception in thread "main" java.lang.ExceptionInInitializerError 
at 
com.bosch.asaw.utilities.HibernateUtil.createSessionFactory(HibernateUtil.java:30) 
at com.bosch.asaw.utilities.HibernateUtil.<init>(HibernateUtil.java:17) 
at com.bosch.asaw.dao.testhibernate.main(testhibernate.java:19) 
    Caused by: java.lang.NullPointerException 
at 
org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:207 
at org.hibernate.service.internal. 
StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75) 
at org.hibernate.service.internal. 
AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:159) 
at  

org.hibernate.service.internal.AbstractServiceRegistryImpl. 
getService(AbstractServiceRegistryImpl.java:131) 
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:78) 
at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2283) 
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2279) 
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1748) 
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1788) 
at 
com.bosch.asaw.utilities.HibernateUtil.createSessionFactory(HibernateUtil.java:25) 
... 2 more 

任何人都可以請告訴我我做錯了什麼。我仔細檢查了Oracle服務器上的端口,sid和主機名。請幫忙。

+0

參考http: //stackoverflow.com/questions/24422993/hibernate-4-3-5-is-not-working-with-oracle10g-databa SE/24430185#24430185 –

回答

0

您是否嘗試構建會話工廠的新代碼?

SessionFactory factory = null; 
try { 
    Configuration configuration = new Configuration(); 
    configuration.configure("resources/hibernate.cfg.xml"); 

    ServiceRegistryBuilder serviceRegistryBuilder = new ServiceRegistryBuilder(); 
    serviceRegistryBuilder.applySettings(configuration.getProperties()); 
    serviceRegistry = serviceRegistryBuilder.buildServiceRegistry(); 

    factory = configuration.buildSessionFactory(serviceRegistry); 
} catch (Throwable ex) { 

    System.err.println("Initial SessionFactory creation failed."); 
    ex.printStackTrace(); 
    throw new ExceptionInInitializerError(ex); 

} 
0

我想在配置文件中的問題。

請參閱以下oracle配置文件。

的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> 
<!-- Database connection settings --> 
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> 
<property name="connection.username">ranga</property> 
<property name="connection.password">ranga</property> 
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:XE</property> 

<!-- JDBC connection pool (use the built-in) --> 
<property name="connection.pool_size">1</property> 

<!-- SQL dialect --> 
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> 

<!-- Disable the second-level cache --> 
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> 

<!-- Echo all executed SQL to stdout --> 
<property name="show_sql">true</property> 

<!-- Drop and re-create the database schema on startup --> 
<property name="hbm2ddl.auto">create</property> 

<mapping resource="com/ranga/mapping/Persom.hbm.xml"/> 

</session-factory> 
</hibernate-configuration>

HibernateUtil.java

package com.ranga.util; 

import org.hibernate.SessionFactory; 
import org.hibernate.cfg.Configuration; 
import org.hibernate.service.ServiceRegistry; 
import org.hibernate.service.ServiceRegistryBuilder; 

public class HibernateUtil { 
    private static final SessionFactory sessionFactory; 
    private static final ServiceRegistry serviceRegistry; 
    static { 
     try { 
      // Create the SessionFactory from hibernate.cfg.xml 
      Configuration configuration = new Configuration(); 
      configuration.configure(); 
      serviceRegistry = new ServiceRegistryBuilder().applySettings(
      configuration.getProperties()).buildServiceRegistry(); 
      sessionFactory = configuration.buildSessionFactory(serviceRegistry); 
     } catch (Throwable ex) { 
      // Make sure you log the exception, as it might be swallowed 
      System.err.println("Initial SessionFactory creation failed." + ex); 
      throw new ExceptionInInitializerError(ex); 
     } 
} 

public static SessionFactory getSessionFactory() { 
    return sessionFactory; 
} 

public static void closeSessionFactory() { 
     if (sessionFactory != null) 
      sessionFactory.close(); 
    } 
} 

如果你想完整的示例程序,從GitHub下載(https://github.com/rangareddy/Hibernate4ExampleUsingMaven/

相關問題