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’s
SessionFactory class by passing your “
hibernate.cfg.xml” file path as an argument into the configure() method:
SessionFactory sessionFactory = new Configuration() .configure("/com/premaseem/hibernate.cfg.xml") .buildSessionFactory(); return sessionFactory;