Friday, April 16, 2010

Run Hibernate without hibernate.cfg.xml file

This is very uncommon speak, run hibernate without hibernate.cfg.xml file?. but it happens in some cases, there is no hard-and-fast rule that you have to have hibernate.cfg.xml file. Yes! Hibernate allows you to do that.

Suppose you want to connect multiple database at runtime, but one database connection at a given point of time. Here may be database driver will be different, database url will be different or user name and password will be different. In this case you have to pass all those configurations to the Hibernate, but question is HOW?

There is a Class named "Configuration" in "org.hibernate.cfg" package. We can use this class to pass dynamic parameters to the Hibernate environment. This is not the case that "Configuration" class is used for passing dynamic parameters to the Hibernate.

In both cases "Configuration" class is used, bi default, hibernate is searched for file name like "hibernate.cfg.xml", read the content and load the parameters to the object of "Configuration" class and use those in hibernate environment.

In our case we don't have "hibernate.cfg.xml" file.
So, you have to write code for loading parameter to the Hibernate environment.

below the small code snippet for that:

No comments:

Post a Comment