5

I'm trying to start project with Hibernate and Maven.

I got such exception:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" 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:2176)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:2157)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:2137)
    at FirstHibernate.com.myhib.CRUDS.CrudsOps.main(CrudsOps.java:15)

Here is the screenshot of my project structure, (hibernate.cfg.xml is in src/): http://imageshack.us/photo/my-images/692/screenshotxba.jpg/

CrudsOps.java

package FirstHibernate.com.myhib.CRUDS;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class CrudsOps {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        SessionFactory sf = new Configuration().configure().buildSessionFactory();
        System.out.println("Cfg and hbm files loaded succesfully");
        Session session = sf.openSession();
        session.beginTransaction();
        System.out.println("Transaction began");

    }

}

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>FirstHibernate</groupId>
  <artifactId>com.myhib</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>com.myhib Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>3.6.8.Final</version>
    </dependency>
    <dependency>
        <groupId>postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.1-901.jdbc4</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>4.2.0.Final</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>com.myhib</finalName>
    <resources>
        <resource>
            <filtering>true</filtering>
            <directory>src/main/resources</directory>
        </resource>
    </resources>
  </build>
</project>

What could be a source of that exception?

1
  • Please check your eclipse classpath file. By default it excludes resource folder Commented Feb 27, 2014 at 9:07

6 Answers 6

16

As @JBNizet said, your hibernate.cfg.xml should be in src/main/resources. In src, it won't be added to your classpath for runtime.

If you are running your project within Eclipse, don't forget in the project preferences in the build path configuration to check that the src/main/resources is not excluded from your class path and is indeed a source folder.

Sign up to request clarification or add additional context in comments.

1 Comment

Excellent analysis @benzonico . Thumbs up for the answer
8

The file should be in the runtime classpath. Maven copies to the target/classes folder the resources that are under src/main/resources. So your config file should be there.

That said, you don't show the code which loads the file, so there might be other problems.

4 Comments

Unfortunately, that didn't help. Updating my post with Java files.
Can you find the hibernate.cfg.xml file under target/classes?
Then either you ddn't rebuild your project with Maven, or you disabled the resource copying somehow in your pom.
I did 'mvn package' command to rebuild project with Maven, is it correct? Posting my 'pom.xml'.
1

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

Comments

0
For the People who are facing this issue while deploying on linux machine

You need to copy hibernate.cfg.xml to classes directory of your war project.

In Eclipse/Maven Project: (During Development)

You need to copy hibernate.cfg.xml to src directory of your Eclipse/Maven project.

Comments

0

If you are working in 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".enter image description here

then this should work

Configuration con = new Configuration().configure("hibernate.cfg.xml");

Comments

0

If you are using Netbeans, put your hibernate.cfg.xml file into the "Build > classes >" directory and it will work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.