52

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
Noor
  • 19,638
  • 38
  • 136
  • 254
  • If you are getting the same error, even though it is placed under `/src`, Then You are going to check artifact(output) folder to make sure it is located under WEB-INF/classes after compiling. – Elbek Oct 01 '12 at 17:17
  • 1
    `/src` is source folder? sometimes `/src/main/class` may stay as a source folder, then you place in `/src/main/class` – Elbek Oct 01 '12 at 17:21
  • stupid question but how is something like org.hibernate.cfg.Configuration referred to by programmers? Is this the project path of the class Configuration? – MrYouMath Jul 20 '18 at 11:29
  • Similar: [*Location of hibernate.cfg.xml in project?*](https://stackoverflow.com/q/18736594/642706) – Basil Bourque Aug 14 '23 at 08:22

9 Answers9

37

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.

Don Cheadle
  • 5,224
  • 5
  • 39
  • 54
  • I have a ant-file which builds me a jar file that I can deploy and it works fine. But if I try to load `new Configuration().configure("file:hibernate.cfg.xml");` it fails everytime doen't matter in which directory I placed hibernate.cfg.xml. If I printout the classpath via URLClassLoader I get `/opt/novell/teaming/apache-tomcat/bin/bootstrap.jar ` which resides on the server-machine. – StellaMaris Mar 18 '16 at 14:08
  • @StellaMaris so have you tried `new Configuration().configure("file:/opt/novell/teaming/apache-tomcat/path/to/hibernate.cfg.xml")`. You need to know where the file is when it's deployed/running to be able to access it that way – Don Cheadle Mar 25 '16 at 20:35
17

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

ksnortum
  • 2,809
  • 4
  • 27
  • 36
12

At the root of your project: /src (at leat as default)

How to know if /src is the sources dir?
When you create a new Java class, it is contained in a package (normally it is called as the same name of the dir where it is created). So, in your class declarion you can see something like this:

package foo;

class MyClass{

In default IDE settings, the class should found under /src/foo/MyClass.java. As you can see, in this scenario /src acts as root sources dir.

manix
  • 14,537
  • 11
  • 70
  • 107
  • is `/src` the dir of your sources project? I suggest to `rebuild` your project. The IDE can help you with this task. – manix Oct 01 '12 at 17:14
  • how can i know this if /src is the dir of my project? – Noor Oct 01 '12 at 17:17
  • 1
    in eclipse, i created a source folder and put all conf files in it, its working now – Noor Oct 01 '12 at 17:27
  • 1
    @Noor, if your are not dealing so much with this tools, the IDE can help you at least in these kinds of problems because they can rebuild, reorganize, and clear several project configs :) – manix Oct 01 '12 at 17:30
4

if it is not web project then do explicitly like that

new Configuration().configure( "pth/to/hibernate.cfg.xml").buildsessionfactory()

Hope this may help

@SiB pointed a link(mkyong's web site). It is explained well there.

Elbek
  • 3,434
  • 6
  • 37
  • 49
  • If my `hibernat.cfg.xml` is in `/Hibernate/src/kasun/hibernate/testing/hibernate.cfg.xml` where `Hibernate` is the project and `kasun.hibernate.testing` is the package. How I specify the `hibernate.cfg.xml` in the above code – Kasun Siyambalapitiya Jul 05 '16 at 07:03
2

The file is suppose to go into the root of your /src dir, while /src is not deployed, everything in it is built/copied out to WEB-INF/classes which IS deployed. Hibernate needs the cfg.xml file in the classpath of your project to load its config settings, your WEB-INF dir is not in your classpath, so if you were to put it there, you'd be hiding it from Hibernate and it wouldn't work.

This thread would tell you how to load the hibernate.cfg.xml from any different path.

Bharat Sinha
  • 13,973
  • 6
  • 39
  • 63
1

If you are using Eclipse, go to Project -> Properties -> Java Build Path -> Source. You can add the new folder where you placed the file, or move the file to the existing folder.

Dino Tw
  • 3,167
  • 4
  • 34
  • 48
1

Place hibernate.cfg.xml under src/ folder or explicitly mention the path in code as:

new Configuration.configure("path of hibernate.cfg.xml").buildsessionfactory()
NatNgs
  • 874
  • 14
  • 25
Arunprasad
  • 21
  • 5
0

CMD+N/CTR+N while you are on Eclips, it will open a dialog-box there you have to dubbel click on the Hibernate folder. It will open a list of files with the XML extenuation. Select the cfg.xml and click on continue and when you are done! click on finish. Eclips will now add the Class name with the cfg.xml file and show it under the SRC folder. GB

bariyaw
  • 41
  • 3
-1

try to place it into "src/main/resources" directory.

Pippi
  • 313
  • 1
  • 4
  • 18