2

我正在尝试将 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 和主机名。请帮忙。

4

2 回答 2

0

我认为配置文件中的问题。

请参阅以下 oracle 配置文件。

休眠.cfg.xml

<?xml 版本="1.0" 编码="UTF-8"?>

<!DOCTYPE hibernate-configuration PUBLIC
"-//休眠/休眠配置 DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<休眠配置>
<会话工厂>
<!-- 数据库连接设置 -->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.username">范围</property>
<property name="connection.password">范围</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:XE</property>

<!-- JDBC连接池(使用内置)-->
<property name="connection.pool_size">1</property>

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

<!-- 禁用二级缓存 -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

<!-- 将所有执行的SQL回显到stdout -->
<property name="show_sql">真</property>

<!-- 在启动时删除并重新创建数据库模式 -->
<property name="hbm2ddl.auto">创建</property>

<映射资源="com/ranga/mapping/Persom.hbm.xml"/>

</会话工厂>
</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/

于 2014-05-13T05:59:19.087 回答
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);

}
于 2014-03-05T07:58:15.710 回答