xml配置后的程序化hibernate配置

时间:2011-04-01 16:47:34

标签: java hibernate configuration

我正在尝试使用hibernate插入记录。当数据库用户名和密码与其他所有数据库一起在hibernate配置文件中时,它工作正常 属性。

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

            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和密码。

提前致谢

2 个答案:

答案 0 :(得分:5)

尝试使用此代码:

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

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

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

答案 1 :(得分:0)

我想知道.setProperty之前的.configure是如何工作的。对我来说,我必须在.setProperty之后配置。我还必须在

中拨打.applySettings(configuration.getProperties())
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()    
        .applySettings(configuration.getProperties())    
        .buildServiceRegistry(); 

让它发挥作用。

也许因为我现在正在使用Hibernate 3.0。