Hibernate 4.3.5不适用于Oracle10g数据库 [英] Hibernate 4.3.5 is not working with Oracle10g Database

查看:178
本文介绍了Hibernate 4.3.5不适用于Oracle10g数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Oracle数据库试用Hibernate 4.3.5。
但是在运行过程中,我遇到了以下问题(与MySQl数据库相同的代码工作正常)。

  org .hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl配置
INFO:HHH000115:Hibernate连接池大小:20(min = 1)
org.hibernate。2014年6月26日上午10:35:46。 engine.jdbc.internal.JdbcServicesImpl配置
警告:HHH000341:无法获取连接元数据:不支持的功能
org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation
INFO:HHH000422:禁用连接为空的上下文LOB创建
线程main中的异常java.lang.NullPointerException $ b $ 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.AbstractServiceRegist ryImpl.initializeService(AbstractServiceRegistryImpl.java:234)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration。
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1843)
at com.cluster.HibernateMain.main(HibernateMain.java:29)

我的代码是

hibernate.cfg.xml

 < hibernate-configuration> 
< session-factory>
<! - 数据库连接属性 - 驱动程序,URL,用户,密码 - >
< 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>建立< / property>


<! - org.hibernate.HibernateException:没有配置CurrentSessionContext! - >
< property name =hibernate.current_session_context_class>线程< / property>

<! - 输出SQL查询,应在生产中禁用 - >
< property name =hibernate.show_sql> true< / property>

<! - 需要方言来让Hibernate知道数据库类型,MySQL,Oracle等
Hibernate 4自动计算出数据库连接的方言元数据 - >
< property name =hibernate.dialect> org.hibernate.dialect.Oracle10gDialect< / property>

<! - 映射文件,我们也可以使用Bean注释 - >
< mapping resource =com \cluster\employee.hbm.xml/>
< / session-factory>
< / hibernate-configuration>

employee.hbm.xml

 <休眠映射> 
< class name =com.cluster.Employeetable =EMPLOYEE>
< id name =idtype =int>
< column name =ID/>
< generator class =increment/>
< / id>
< property name =nametype =java.lang.String>
< column name =NAME/>
< / property>
< property name =roletype =java.lang.String>
< column name =ROLE/>
< / property>
< property name =insertTimetype =timestamp>
< column name =insert_time/>
< / property>
< / class>
< / hibernate-mapping>

而客户端代码是


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



//从hibernate.cfg.xml创建SessionFactory
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);

//获取会话
会话会话= sessionFactory.getCurrentSession();


//开始交易
session.beginTransaction();
//保存模型对象
session.save(emp);
//提交事务
session.getTransaction()。commit();
System.out.println(Employee ID =+ emp.getId());

//终止会话工厂,否则程序不会结束
sessionFactory.close();
}

}

我使用的是



什么我缺少的东西?
在此先感谢。

解决方案

我通过删除 ojdbc14.jar 并添加 ojdbc6.jar
由于Hibernate 4使用JDBC4.0实现,它不存在于 ojdbc14.jar 中,但该实现存在于 ojdbc6.jar 中。


I am trying out Hibernate 4.3.5 with Oracle Database. But during run I am getting the following problem(The same code is working fine with MySQl database).

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)

My code is

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>

employee.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>

And the client code is

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();
    }

}

Jars what I am using are

What the thing I am missing ? Thanks in advance.

解决方案

I solved the problem by removing ojdbc14.jar and adding ojdbc6.jar. Because as Hibernate 4 is using JDBC4.0 implementation which is not present in ojdbc14.jar but this implementation is present in ojdbc6.jar.

这篇关于Hibernate 4.3.5不适用于Oracle10g数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆