Multiple databases specified in hibernate.properties
Looking at a little tool project and I’m going to give Hibernate another kick at the can. Used it before on another small project in the past.
On this project, I’ll be connecting to 2 different datasources. The first datasource I’ll just be doing read-only queries. The second datasource I’ll be inserting data based on what I read & rearrange from the first datasource. I’m looking at how to specify using multiple databases. I can’t see any way to give a name to a datasource within hibernate.properties to distinguish between the two.
One way I was thinking I might have to do this is create 2 different hibernate.properties files (ie, ds1.properties & ds2.properties) and then passing whichever one I needed at that time into Configuration.setProperties(). Is there any smarter/easier way to do this? I’ve got this working and it seems alright.
Update: this didn’t turn out to be too difficult. I just make 2 different calls to my cfg files when I need my connection. Maybe there’s a more elegant way of doing this, but it works fine for me.
foo.java:
SessionFactory sf = new Configuration().configure("/configA.cfg.xml").buildSessionFactory();
Session sess = sf.openSession();bar.java:
SessionFactory sf = new Configuration().configure("/configB.cfg.xml").buildSessionFactory();
Session sess = sf.openSession();

July 13th, 2004 at 3:44 pm
I ran into this on a recent project — what I ended up doing is specifying everything but the datasource in hibernate.config.xml, and then specifying the datasource through a set of properties externally which I passed into the configuration.
I’d then call config.addProperties() for the datasource properties that I was pulling out of another configuration file, in my instance.
You could do something similar, if you want to specify some properties centrally and some on a per-connection basis.
August 9th, 2004 at 10:34 pm
Hi Geoffrey,
How many hibernate.config.xml do you use? can you post your hibernate.config.
xml and property files and some code as well? it’s really help !
Thanks
Sharon
January 10th, 2006 at 4:21 pm
I would like to know how you got this working as well. I have two SessionManager classes each pointing to a different Hibernate config xml file.
One is named hibernate.cfg.xml the other is CarHibernate.cfg.xml
I can seem to get it working?
Any help would be appreciated?
November 8th, 2007 at 4:49 am
Hi,
You may just change properties of configuration object at runtime,
like this
configuration.setProperty(Environment.URL, “jdbc:…://path/to/db”);
//set any properties here which are different for these databases
Session session = configuration.buildSessionfactory().openSession();
worked for me.
but I get error when trying to save object retrieved from db1 to db2.
Does anyone have an idea how to eliminate this problem?
thank you very much in advance.
November 26th, 2008 at 5:45 am
I also have the similar kind of problem. But in my case, mapping files in hb1.cfg.xml are referring to the classes in hb2.cfg.xml as they have many to one relationship between them.
But in session factory i can configure only one hb.cfg.xml file. It results into org.hibernate.MappingException: An association from the table XXX refers to an unmapped class: YYY.
Any idea how to resolve it.