Hibernate : Hibernate Configuration

Hibernate requires to know in advance where to find the mapping information that defines how your Java classes relate to the database tables. Hibernate also requires a set of configuration settings related to database and other related parameters. All such information is usually supplied as a standard Java properties file called hibernate.properties, or as an XML file named hibernate.cfg.xml.


I will consider XML formatted file hibernate.cfg.xml to specify required Hibernate properties in my examples. Most of the properties take their default values and it is not required to specify them in the property file unless it is really required. This file is kept in the root directory of your application’s classpath.If you place this configuration file into a different directory, you may encounter the following error :

Initial SessionFactory creation failed.org.hibernate.HibernateException: 
/hibernate.cfg.xml not found

To ask Hibernate look for your “hibernate.cfg.xml” file in other directory, you can modify the default Hibernate’sSessionFactory class by passing your “hibernate.cfg.xml” file path as an argument into the configure() method:

            SessionFactory sessionFactory = new Configuration()
            .buildSessionFactory();   return sessionFactory;

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s