Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to place hibernate.cfg.xml?

My project is like this:

/src/main/java
     -thegamers
         -app.java
         -hibernateutil.java

can someone tell me where to put the hibernate.cfg.xml?

because I'm getting this error:

Initial SessionFactory creation failed.org.hibernate.HibernateException: hibernate.cfg.xml not found
Exception in thread "main" java.lang.ExceptionInInitializerError
    at thegamers.HibernateUtil.buildSessionFactory(HibernateUtil.java:17)
    at thegamers.HibernateUtil.<clinit>(HibernateUtil.java:8)
    at thegamers.App.main(App.java:15)
Caused by: org.hibernate.HibernateException: hibernate.cfg.xml not found
    at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:170)
    at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:2149)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:2130)
    at thegamers.HibernateUtil.buildSessionFactory(HibernateUtil.java:13)
    ... 2 more
like image 944
Noor Avatar asked Oct 01 '12 17:10

Noor


People also ask

Where is hibernate cfg XML located?

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.

What is the use of hibernate cfg XML?

These configurations contain the mapping information that provides different functionalities to Java classes. Generally, we provide database related mappings in the configuration file. Hibernate facilitates to provide the configurations either in an XML file (like hibernate. cfg.

Is it necessary to use hibernate cfg XML for configuration?

Basically you are setting all the required properties via your properties object so there is no real need to tell Hibernate to look for a hibernate. cfg. xml file which is exactly what the configure() method does.


2 Answers

The config file hibernate.cfg.xml needs to be on the classpath.

This can be accomplished in different ways, depending on your project.

  • For a web-app WAR project (you are running the program in a Servlet container): placing it in WEB-INF/classes will work as files in WEB-INF/classes are visible on the classpath when app is running in container.

  • For a Maven-style project (not running the program in a Servlet container): placing it in /src/main/resources/ will work

  • For otherwise, try in the src/ directory.

like image 84
Don Cheadle Avatar answered Nov 10 '22 00:11

Don Cheadle


I'm using maven, and it didn't work for me until I put hibernate.cfg.xml in src/main/resources.

like image 31
ksnortum Avatar answered Nov 09 '22 23:11

ksnortum