我正在尝试将 Hibernate 4.2 与 Oracle 11g 一起使用。以下是我的 hibernate.cfg.xml 文件:
<?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">abcde@1234</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 数据库和相关的 hibernate.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 和主机名。请帮忙。