Hibernate 4.3.5 不适用于 Oracle10g 数据库

Posted

技术标签:

【中文标题】Hibernate 4.3.5 不适用于 Oracle10g 数据库【英文标题】:Hibernate 4.3.5 is not working with Oracle10g Database 【发布时间】:2014-08-16 20:41:12 【问题描述】:

我正在试用带有 Oracle 数据库的 Hibernate 4.3.5。 但是在运行期间我遇到了以下问题(相同的代码在 mysql 数据库中运行良好)。

org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
Jun 26, 2014 10:35:46 AM org.hibernate.engine.jdbc.internal.JdbcServicesImpl configure
WARN: HHH000341: Could not obtain connection metadata : Unsupported feature
Jun 26, 2014 10:35:46 AM org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation
INFO: HHH000422: Disabling contextual LOB creation as connection was null
Exception in thread "main" java.lang.NullPointerException
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:244)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1885)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1843)
at com.cluster.HibernateMain.main(HibernateMain.java:29)

我的代码是

hibernate.cfg.xml

   <hibernate-configuration>
    <session-factory>
    <!-- Database connection properties - Driver, URL, user, password -->
        <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
        <property name="hibernate.connection.username">hr</property>
        <property name="hibernate.connection.password">hr</property>
        <property name="hibernate.hbm2ddl">create</property>


        <!-- org.hibernate.HibernateException: No CurrentSessionContext configured! -->
        <property name="hibernate.current_session_context_class">thread</property>

        <!-- Outputs the SQL queries, should be disabled in Production -->
        <property name="hibernate.show_sql">true</property>

        <!-- Dialect is required to let Hibernate know the Database Type, MySQL, Oracle etc
            Hibernate 4 automatically figure out Dialect from Database Connection Metadata -->
        <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property> 

        <!-- mapping file, we can use Bean annotations too -->
        <mapping resource="com\cluster\employee.hbm.xml" />
    </session-factory>
</hibernate-configuration>

员工.hbm.xml

<hibernate-mapping>
    <class name="com.cluster.Employee" table="EMPLOYEE">
        <id name="id" type="int">
            <column name="ID" />
            <generator class="increment" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="NAME" />
        </property>
        <property name="role" type="java.lang.String">
            <column name="ROLE" />
        </property>
        <property name="insertTime" type="timestamp">
            <column name="insert_time" />
        </property>
    </class>
</hibernate-mapping>

而客户端代码是

public class HibernateMain 

    public static void main(String[] args) 
        Employee emp = new Employee();
        emp.setName("Amr");
        emp.setRole("President");
        emp.setInsertTime(new Date());



     // Create the SessionFactory from hibernate.cfg.xml
        Configuration configuration = new Configuration();
        configuration.configure();
        System.out.println("Hibernate Configuration loaded");

        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
        System.out.println("Hibernate serviceRegistry created");

        SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);

        //Get Session
        Session session = sessionFactory.getCurrentSession();


        //start transaction
        session.beginTransaction();
        //Save the Model object
        session.save(emp);
        //Commit transaction
        session.getTransaction().commit();
        System.out.println("Employee ID="+emp.getId());

        //terminate session factory, otherwise program won't end
        sessionFactory.close();
    


我正在使用的罐子是

我错过了什么? 提前致谢。

【问题讨论】:

【参考方案1】:

我通过删除 ojdbc14.jar 并添加 ojdbc6.jar 解决了这个问题。 因为 Hibernate 4 使用的是 ojdbc14.jar 中不存在的 JDBC4.0 实现,但 ojdbc6.jar 中存在此实现。

【讨论】:

以上是关于Hibernate 4.3.5 不适用于 Oracle10g 数据库的主要内容,如果未能解决你的问题,请参考以下文章

Hibernate 4 字节码增强不适用于脏检查优化

@Where with @SecondaryTable 不适用于 Hibernate

事务不适用于 Spring 3.1 – H2 – junit 4 – hibernate 3.2

多模式 DDL 外键生成不适用于 Hibernate

Hibernate 函数 lower 和 upper 不适用于波兰语特殊字符

Hibernate AutoGenerated 值不适用于 Jackson 映射器