• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Connecting to Multiple databases in hibernate

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
priya aggrawal
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 544
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
You know it is dark times when the trees riot. I think this tiny ad is their leader:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic