我想在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和主機名。請幫忙。
參考http: //stackoverflow.com/questions/24422993/hibernate-4-3-5-is-not-working-with-oracle10g-databa SE/24430185#24430185 –