2014-09-10 74 views
-2

我想用Oracle 11g配置Hibernate 4.3.6,但我无法配置...我是
在创建会话时得到空指针异常... i我把所有的配置 和程序文件,请帮我弄失败的根本原因休眠4.3.6使用Oracle 11g配置

hibernate.cfg.xml 

    <hibernate-configuration> 
    <session-factory> 

    <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property> 
    <property name="hibernate.connection.url">jdbc:oracle:thin:@127.0.0.1:1521:XE</property> 
    <property name="username">TEST</property> 
    <property name="password">ORACLE</property> 
    <property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property> 
    <property name="show_sql">true</property> 
    <property name="hibernate.connection.pool_size">5</property> 
    </session-factory> 
</hibernate-configuration> 

HibernateUtil.java 

public class HibernateUtil { 
    private static SessionFactory sessionFactory; 
    private static ServiceRegistry serviceRegistry; 
    public static SessionFactory createSessionFactory() { 
     try{ 
      Configuration configuration = new Configuration(); 
      configuration.configure("hibernate.cfg.xml"); 
      serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
        configuration.getProperties()).build(); 
      sessionFactory = configuration.buildSessionFactory(serviceRegistry); 
      return sessionFactory; 
     } 
     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; 
    } 
} 

FetchTest.java 

public class FetchTest { 
<br> 
    public static void main(String a[]){<br> 
     System.out.println("*********************** Inside Main ***********************"); 

     <br><br> 
      Session session = HibernateUtil.getSessionFactory().openSession(); 
    } 
} 

Output : - 

*********************** Inside Main *********************** 
Exception in thread "main" java.lang.NullPointerException 
    at com.naveen.org.FetchTest.main(FetchTest.java:18) 

Please give your suggestions how to get ride from this.....? 

回答

2

NullPointerException来,如果你没有被实例化的对象上执行的操作。

所以,你同时做得到此异常:

HibernateUtil.getSessionFactory().openSession(); 

这无非是:

sessionFactory.openSession(); 

按照你在你的问题张贴代码sessionFactory应该是null因为你创造了一个static变量是这样的:

private static SessionFactory sessionFactory; 

并使用方法HibernateUtil.getSessionFactory()直接访问它,而无需在代码中的任何位置调用createSessionFactory()

PS:您需要在SO中直接发布基本问题之前进行一些基本分析。