-->
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.  [ 3 posts ] 
Author Message
 Post subject: Programmatically override settings specified in config
PostPosted: Wed Jun 11, 2008 2:20 am 
Newbie

Joined: Mon Mar 24, 2008 12:33 pm
Posts: 3
Hello.

I need to programmatically override "default" settings, specified in hibernate.cfg.xml. For example, I have:

...
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/db1</property>
<property name="connection.username">user</property>
<property name="connection.password">pass</property>
<property name="connection.useUnicode">yes</property>
<property name="connection.characterEncoding">utf8</property>
...

I want to set in runtime another connection.url. I do next:

Configuration configuration = new Configuration()
.configure()
.setProperty("connection.url", "jdbc:mysql://192.168.2.1/db1");
sessions = configuration.buildSessionFactory();

But hibernate uses values from hibernate.cfg.xml. How can I solve this problem?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 13, 2008 4:08 pm 
Expert
Expert

Joined: Thu May 26, 2005 9:19 am
Posts: 262
Location: Oak Creek, WI
Look into this for directions...

http://forum.hibernate.org/viewtopic.ph ... 5dec715b73

_________________
RamnathN
Senior Software Engineer
http://www.linkedin.com/in/ramnathn
Don't forget to rate.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 14, 2008 9:30 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
This should be as simple as doing something like this:




Code:
Configuration configuration = new Configuration();
configuration.setProperty("hibernate.connection.driver_class", org.hsqldb.jdbcDriver.class.getName());
configuration.setProperty("hibernate.connection.url", "jdbc:hsqldb:abc:123");
configuration.setProperty("hibernate.connection.username", "sa");
configuration.setProperty("hibernate.connection.password", "");
configuration.setProperty("hibernate.dialect", "net.sf.hibernate.dialect.HSQLDialect");
Session session = configuration.buildSessionFactory().openSession();



However, you must not call configure on the configuration object before setting the properties. You do it after. This works equally well with the annotationconfiguration object.

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.