-->
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.  [ 8 posts ] 
Author Message
 Post subject: Variables in hibernate.cfg.xml
PostPosted: Wed Jun 02, 2010 3:08 am 
Regular
Regular

Joined: Wed Mar 10, 2010 4:48 am
Posts: 106
In my config file I have this:
Code:
<property name="hibernate.connection.url">jdbc:mysql://IP:PORT/DB_NAME</property>

Can I make IP variable? and can I set this variable in my java main?


Top
 Profile  
 
 Post subject: Re: Variables in hibernate.cfg.xml
PostPosted: Wed Jun 02, 2010 4:04 am 
Newbie

Joined: Sat Mar 27, 2010 2:43 am
Posts: 8
yes you can use the IP address instead of the computer name
like localhost by 127.0.0.1 and then the service name or DB name which
like 127.0.0.1/service

And are you asking that you need to set this varaible in
Code:
public static void main ( String args[] )
{
...
}


In the above code?


Top
 Profile  
 
 Post subject: Re: Variables in hibernate.cfg.xml
PostPosted: Wed Jun 02, 2010 4:21 am 
Regular
Regular

Joined: Wed Mar 10, 2010 4:48 am
Posts: 106
I quess I wasn't clear.

Now I have something like this:
Code:
<property name="hibernate.connection.url">jdbc:mysql://192.168.0.2:3306/db</property>
In stead of 192.168.0.2 I want to use a variable, which I will set before the hibernate.cfg.xml file is used.


Top
 Profile  
 
 Post subject: Re: Variables in hibernate.cfg.xml
PostPosted: Wed Jun 02, 2010 4:31 am 
Newbie

Joined: Sat Mar 27, 2010 2:43 am
Posts: 8
yes you can use the IP address instead of the system name.

And as far as the editing some file which will be having this details, yes you can edit that file and set whatever value you want. For that you need to open that particular file, traverse till the particular property and change the machine name to the respective IP address.


Top
 Profile  
 
 Post subject: Re: Variables in hibernate.cfg.xml
PostPosted: Wed Jun 02, 2010 4:54 am 
Regular
Regular

Joined: Wed Mar 10, 2010 4:48 am
Posts: 106
I have this working now.
I use a BufferedReader and a BufferedWriter to modify the url in the hibernate.cfg.xml file.
The problem is that Hibernate reads the config file before I modify it.

Does anyone knows when the config file is read or is modifying not possible after compilation?


Top
 Profile  
 
 Post subject: Re: Variables in hibernate.cfg.xml
PostPosted: Wed Jun 02, 2010 5:20 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Hi the usual way people do this is by using ANT filters or Maven profiles to replace some tokens depending on deployment target (test/production/...) in the configuration files.

If you need something more flexible, you don't need to have a hibernate.cfg.xml file at all, you can create the configuration instance programmatically: new Configuration() and then set the properties on it. You could read almost all properties from a properties file, and add some dynamically reading environment variables or program arguments.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: Variables in hibernate.cfg.xml
PostPosted: Wed Jun 02, 2010 5:54 am 
Regular
Regular

Joined: Wed Mar 10, 2010 4:48 am
Posts: 106
Thanks a lot.

I do it now using:
Code:
Configuration config = new Configuration();
config.setProperty("hibernate.connection.url", "jdbc:mysql://" + SystemConfig.URL);
config.setProperty("hibernate.connection.username", SystemConfig.USERNAME);
config.setProperty("hibernate.connection.password", SystemConfig.PASSWORD);
sessionFactory = config.configure().buildSessionFactory();


Top
 Profile  
 
 Post subject: Re: Variables in hibernate.cfg.xml
PostPosted: Wed Jun 02, 2010 6:04 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
cool.
FYI there are some useful extensions of Configuration, like AnnotationConfiguration and EJB3Configuration.

_________________
Sanne
http://in.relation.to/


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