greenLizard
greenLizard

Reputation: 2346

hibernate.cfg.xml not found

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 :)

Upvotes: 15

Views: 65963

Answers (8)

rellabacode
rellabacode

Reputation: 176

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

Upvotes: 0

makson
makson

Reputation: 2243

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

Upvotes: 0

sam
sam

Reputation: 2004

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");

Upvotes: 3

Arthur
Arthur

Reputation: 608

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);

Upvotes: 3

Dila Gurung
Dila Gurung

Reputation: 1764

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.

Upvotes: 0

user1945481
user1945481

Reputation:

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.

Upvotes: 19

ruakh
ruakh

Reputation: 183211

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.

Upvotes: 15

Dave Newton
Dave Newton

Reputation: 160170

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

Upvotes: 5

Related Questions