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.
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.
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.
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.