-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: change user and password to connect db using hibernate
PostPosted: Tue Aug 08, 2006 2:42 pm 
Newbie

Joined: Fri Aug 04, 2006 9:34 am
Posts: 5
Hi, i have this problem, i want to connect to database using differents users name and password, i try to chage it at run time with this code:

Properties prop = cfg.getProperties();
prop.put("hibernate.connection.password",pass);
prop.put("hibernate.connection.username",usr);
cfg.getProperties().clear();
cfg.setProperties(prop);
sessionFactory = cfg.buildSessionFactory();

but when sessionFactory is created it still have the old user name and pass (same in hibernate.cfg.xml)

Please someone help me.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 08, 2006 3:31 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
Just some basic Java problems:

1) You really don't want to call the put method on Properties, it delegates to the underlying HashTable in an unsafe manner (try setProperty).

2) The Properties you load with cfg.getProperties() in the first line is the same object you write to in line 2 and 3 and then subsequently clear in line 4, which you then set in line 5! Oops! ;)

3) You are much better off using this methodology:
Code:
Configuration cfg = new Configuration();
cfg.addResource("mypackage/Customer.hbm.xml");
System.setProperty("hibernate.connection.password",pass);
System.setProperty("hibernate.connection.username",usr);
cfg.setProperties(System.getProperties());
sessionFactory = cfg.buildSessionFactory();


Although, I suppose you could still do:
Code:
Configuration cfg = new Configuration();
cfg.addResource("mypackage/Customer.hbm.xml");
cfg.setProperties(System.getProperties());
cfg.getProperties().setProperty("hibernate.connection.password",pass);
cfg.getProperties().setProperty("hibernate.connection.username",usr);
sessionFactory = cfg.buildSessionFactory();


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 08, 2006 4:22 pm 
Newbie

Joined: Fri Aug 04, 2006 9:34 am
Posts: 5
Ananasi, thanks for you help, you were right I made a mistake with the my other code, I change it, but still have a problem, when I create a sessionFactory, this error is given:

org.hibernate.HibernateException: database product name cannot be null

I´m using hibernate 3 with myEclipse plugin and this code is in HibernateSessionFactory.java

Thanks!!!!!!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 08, 2006 4:41 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
The rest of the properties must be loaded in some manner by the Configuration object. Usually, this is instantiated by calling Configuration.configure(), which will read the hibernate.cfg.xml file and copy the properties found therein to the System.properties.

Try one of the following:

1) Configure from hibernate.cfg.xml and override:
Code:
Configuration cfg = new Configuration();
cfg.configure();
System.setProperty("hibernate.connection.password",pass);
System.setProperty("hibernate.connection.username",usr);
cfg.setProperties(System.getProperties());
sessionFactory = cfg.buildSessionFactory();


2) Configure it all in code:
Code:
Configuration cfg = new Configuration();
cfg.configure();
System.setProperty("hibernate.connection.password",pass);
System.setProperty("hibernate.connection.username",usr);
System.setProperty("hibernate.connection.driver_class", driver_class);
System.setProperty("hibernate.connection.url", driver_url);
System.setProperty("hibernate.dialect", dialect);
// etc, etc, for all properties
cfg.setProperties(System.getProperties());
sessionFactory = cfg.buildSessionFactory();


Good luck and good coding!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 08, 2006 5:01 pm 
Newbie

Joined: Fri Aug 04, 2006 9:34 am
Posts: 5
Hi again, i fill in blanks pass an user in hibernate.cfg.xml, and i code this,

System.setProperty("hibernate.connection.password",pass);
System.setProperty("hibernate.connection.username",usr);
cfg.configure();
sessionFactory = cfg.buildSessionFactory();

now i have this error:

Exception in thread "main" java.lang.RuntimeException: org.hibernate.exception.GenericJDBCException: Cannot open connection
at mapeo.Entity.findAll(Entity.java:62)
at prueba.TestConexion.main(TestConexion.java:71)
Caused by: org.hibernate.exception.GenericJDBCException: Cannot open connection
at org.hibernate.exception.ErrorCodeConverter.handledNonSpecificException(ErrorCodeConverter.java:92)
at org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:80)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:29)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:301)
at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:110)
at org.hibernate.jdbc.AbstractBatcher.prepareQueryStatement(AbstractBatcher.java:88)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1162)
at org.hibernate.loader.Loader.doQuery(Loader.java:390)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:218)
at org.hibernate.loader.Loader.doList(Loader.java:1593)
at org.hibernate.loader.Loader.list(Loader.java:1577)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:395)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:271)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:844)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:74)
at mapeo.Entity.findAll(Entity.java:59)
... 1 more
Caused by: java.sql.SQLException: argumentos no válidos en la llamada
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:134)
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:179)
at oracle.jdbc.dbaccess.DBError.check_error(DBError.java:1160)
at oracle.jdbc.ttc7.TTC7Protocol.logon(TTC7Protocol.java:184)
at oracle.jdbc.driver.OracleConnection.<init>(OracleConnection.java:365)
at oracle.jdbc.driver.OracleDriver.getConnectionInstance(OracleDriver.java:547)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:347)
at java.sql.DriverManager.getConnection(DriverManager.java:525)
at java.sql.DriverManager.getConnection(DriverManager.java:140)
at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:110)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:298)
... 13 more


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 09, 2006 9:13 am 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
Check the order of your statements. You should do a configure(), then override the properties from the hibernate.cfg.xml file and then do the cfg.setProperties(). The configure() method will load all properties from the hibernate.cfg.xml file and overwrite any existing properties therein.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 10, 2006 9:53 am 
Newbie

Joined: Fri Aug 04, 2006 9:34 am
Posts: 5
Thanks my friend, with your help I can resolve my problem. Thanks a lot!!!

Muchas gracias amigo!!!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.