lali
lali

Reputation: 1

Programmatic hibernate configuration after xml configuration

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)

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

Thanks in advance

Upvotes: 0

Views: 1795

Answers (2)

Siddharth
Siddharth

Reputation: 9574

I wonder how .configure before .setProperty worked. For me I had to .configure after .setProperty. Also I had to call .applySettings(configuration.getProperties()) in

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

for it to work.

Maybe since I am using Hibernate 3.0 now.

Upvotes: 0

GuruKulki
GuruKulki

Reputation: 26418

Try with this code:

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

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

You missed adding hibernate prefix for the property names.

Upvotes: 5

Related Questions