xml配置后的程序化hibernate配置 [英] Programmatic hibernate configuration after xml configuration

查看:157
本文介绍了xml配置后的程序化hibernate配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用hibernate插入记录。它工作正常,当数据库usernanme和密码是在hibernate配置文件以及所有其他
属性。

I am trying to insert a record using hibernate. It works fine when database usernanme and password are in the hibernate config file along with all other properties.

但是当我从配置文件的用户名和密码中删除它时,我无法插入。我可以做错什么?

But when I remove it from the username and password from the config file, I am unable to insert. What could I be doing wrong?

            Configuration c = new Configuration();
    c.configure();

    c.setProperty("connection.username", "abc" );
    c.setProperty("connection.password", "secret" ); 
    SessionFactory sessFact =   c.buildSessionFactory();
    Session sess = sessFact.openSession();

    Transaction tx = sess.beginTransaction();
            sess.save(inf);
    tx.commit();
    sess.close();







org.hibernate.exception.GenericJDBCException: Cannot open connection
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:140)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:128)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:52)
at org.hibernate.jdbc.ConnectionManager.openConnection

....

Caused by: java.sql.SQLException: invalid arguments in call
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:208)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:236)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:414)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:165)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:35)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:801)
at java.sql.DriverManager.getConnection(DriverManager.java:525)
at java.sql.DriverManager.getConnection(DriverManager.java:140)
at     org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:133)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:446)

我想从xml配置文件中选择大多数属性,除了uid和密码。

I want to pick most of the properties from the xml config file, except the uid and password.

/ p>

Thanks in advance

推荐答案

尝试此代码:

    Configuration c = new Configuration();
    c.configure();

    c.setProperty("hibernate.connection.username", "abc" );
    c.setProperty("hibernate.connection.password", "secret" ); 

您错过为属性名称添加hibernate前缀。

You missed adding hibernate prefix for the property names.

这篇关于xml配置后的程序化hibernate配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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