aspose file tools*
The moose likes Object Relational Mapping and the fly likes Connecting to Multiple databases in hibernate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Connecting to Multiple databases in hibernate" Watch "Connecting to Multiple databases in hibernate" New topic
Author

Connecting to Multiple databases in hibernate

priya aggrawal
Greenhorn

Joined: Nov 12, 2010
Posts: 6
Hi,

I want to connect to multiple databases in hibernate using single cfg.xml file. is it possible to implement this by creating multiple session factory objects and use id mechanism to load the db configuration dynamically.

please suggest.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Not from a single config file, no. But look at the methods available to you in the Configuration class - there is quite a lot of scope how you configure the SessionFactory.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
priya aggrawal
Greenhorn

Joined: Nov 12, 2010
Posts: 6
so you mean to say i need to have mutiple cfg files and multiple session factory objects. I don't want to use setter methods for defining the DB configurations. My requirement is i want to change the DB at the runtime.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Yes, you need multiple SessionFactories. You may need multiple configuration files, depending on how you configure these (see my earlier post). You will need to write the DAO logic to know which SessionFactory to go to depending on the entity.

amit punekar
Ranch Hand

Joined: May 14, 2004
Posts: 512
Hi,
If you can let us know what is the requirement then it might be more helpful.
In case you need to connect to different databases because you are promoting your code through different development environments then that is easily achievable. While building the code for particular environment you can replace the DB using ant scripts and build.properties file.

However if the requirement is like having multiple databases connections in a single environment then you will need different SessionFactories for each of the DB as mentioned by Paul earlier.

Regds,
Amit
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Connecting to Multiple databases in hibernate