hibernate.cfg.xml not found
Asked Answered
P

8

15

I am new to Hibernate, reading this book "Java persistence with Hibernate" and I am trying to implement the example from there. So far my Ant build is successful, but when I try to execute the class containing the main method I am getting this error message:

19-Nov-2011 18:40:09 org.hibernate.cfg.Environment <clinit>
INFO: Hibernate 3.2.3
19-Nov-2011 18:40:09 org.hibernate.cfg.Environment <clinit>
INFO: hibernate.properties not found
19-Nov-2011 18:40:09 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: Bytecode provider name : cglib
19-Nov-2011 18:40:09 org.hibernate.cfg.Environment <clinit>
INFO: using JDK 1.4 java.sql.Timestamp handling
19-Nov-2011 18:40:09 org.hibernate.cfg.Configuration configure
INFO: configuring from resource: /hibernate.cfg.xml
19-Nov-2011 18:40:09 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: Configuration resource: /hibernate.cfg.xml
Exception in thread "main" java.lang.ExceptionInInitializerError
    at persistence.HibernateUtil.<clinit>(Unknown Source)
    at hello.Driver.main(Unknown Source)
Caused by: org.hibernate.HibernateException: /hibernate.cfg.xml not found
    at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:147)
    at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:1405)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1427)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1414)
    ... 2 more

It is clear that hibernate can't find my config file, which is located in the root dir.

Project

+lib
<all required libraries>
+src
  +hello
    HelloWorld.java
    Message.java
    message.hbm.xml
  +persistence
    HibernateUtil.java
build.xml
hibernate.cfg.xml

My the complete source code can be found here: http://pastebin.com/bGDUrxUf

I have a running MySQL server with a database hibernateapp and table messages

Thanks :)

Petronilapetronilla answered 19/11, 2011 at 18:47 Comment(0)
O
15

Your hibernate.cfg.xml needs to be inside the src directory; otherwise it's not covered by Ant's copymetafiles target, so it won't end up in your compiled classpath.

Ounce answered 19/11, 2011 at 18:52 Comment(4)
I did move it to src, recompile using ant and run the class holding the main method as Java application and got the same error ? Do I need to set some GLOBAL PATH ?Petronilapetronilla
@Agop: Make sure that it's ending up in the root of one the directories on your classpath. For example, if you're running java -classpath lib/foo.jar:lib/bar.jar:. hello.HelloWorld, then you need to make sure it's ending up in ..Ounce
I am running it using Eclipse,so I believe the class path should be fine ?Petronilapetronilla
I'm not saying "your classpath is wrong", I'm saying "whatever your classpath is, make sure your hibernate.cfg.xml is ending up in there".Ounce
H
19

The hibernate.cfg.xml file shoul be in root directory of the classpath of your project. If you using Maven then make sure it should be like src > resources > hibernate.cfg.xml.

Humbertohumble answered 3/1, 2013 at 12:3 Comment(0)
O
15

Your hibernate.cfg.xml needs to be inside the src directory; otherwise it's not covered by Ant's copymetafiles target, so it won't end up in your compiled classpath.

Ounce answered 19/11, 2011 at 18:52 Comment(4)
I did move it to src, recompile using ant and run the class holding the main method as Java application and got the same error ? Do I need to set some GLOBAL PATH ?Petronilapetronilla
@Agop: Make sure that it's ending up in the root of one the directories on your classpath. For example, if you're running java -classpath lib/foo.jar:lib/bar.jar:. hello.HelloWorld, then you need to make sure it's ending up in ..Ounce
I am running it using Eclipse,so I believe the class path should be fine ?Petronilapetronilla
I'm not saying "your classpath is wrong", I'm saying "whatever your classpath is, make sure your hibernate.cfg.xml is ending up in there".Ounce
F
5

It shouldn't be in your root directory, it should be on your classpath.

Fujimoto answered 19/11, 2011 at 18:51 Comment(0)
B
3

You can load hibernate.cfg.xml from a different directory (not necessarily the classpath) using the configure(File configFile) method that takes the hibernateConfig File argument. (note, I am using hibernate 4.3.7)

Like this:


String hibernatePropsFilePath = "/etc/configs/hibernate.cfg.xml";

File hibernatePropsFile = new File(hibernatePropsFilePath);

Configuration configuration = new Configuration(); 
configuration.configure(hibernatePropsFile);

StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());

ServiceRegistry serviceRegistry = serviceRegistryBuilder.build();

SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
Benner answered 27/12, 2014 at 10:7 Comment(0)
G
3

If you are working on Intellij Idea then make a folder named "resources" under src\main\java. Open Module setting of your project, select "Modules" from left and in the "sources" tab select the newly created "resources" folder and mark it as "Resources". Create thehibernate.cfg.xml file inside this newly created "resources" folder.enter image description here

then this should work

Configuration con = new Configuration().configure("hibernate.cfg.xml");
Gardiner answered 23/10, 2017 at 7:25 Comment(0)
H
0

Though it it is late, The solution is you need to put this configuration file inside resource folder(projectxxxx->Resources) provided its a maven project.

Hobgoblin answered 25/12, 2015 at 6:49 Comment(0)
S
0

The XML configuration file is by default expected to be in the root of your CLASSPATH.

You can select a different XML or path configuration file using:

        SessionFactory sessionFactory;
        try {
            Logger log = Logger.getLogger(LOG);
            final String HIB_CONFIG = "/path/to/hibernate.cfg.xml";

            final File hibernate = new File(HIB_CONFIG);
            log.info("Try to init SessionFactory: " + HIB_CONFIG);

            // Create the SessionFactory from hibernate.cfg.xml
            if (hibernate.exists()) {
                log.info("File exists. Init with custom file.");
                sessionFactory = new Configuration()
                        .configure(hibernate)
                        .buildSessionFactory();
            } else {
                log.info("File doesn't exist. Init with default project file.");
                sessionFactory = new Configuration().configure().buildSessionFactory();
            }
        } catch (Throwable ex) {
            throw new ExceptionInInitializerError(ex);
        }

For more information look to session-configuration

Sulfonate answered 2/10, 2019 at 11:40 Comment(0)
B
0

In my case i was launching a main class of a Maven project from Eclipse (Run App). I needed to put the file at src/main/java and after compilation (Project > Clean or mvn clean package), the file was placed at the root of the project/target/classes folder and the program was able to find it

Barghest answered 6/2 at 16:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.