1

I am trying to learn Hibernate + Maven. I am trying to connect to a PostgreSQL database, but I am getting the following error:

Initial SessionFactory creation failed.org.hibernate.HibernateException: hibernate.cfg.xml not found

hibernate.cfg.xml file is already under src/main/resources folder and i have already added that folder as a source folder.

Here is the code for session factory initialization:

Configuration  configuration = new Configuration().configure("hibernate.cfg.xml");
        return  configuration.buildSessionFactory();

Edit : Here is the pom.xml

    <modelVersion>4.0.0</modelVersion>

  <groupId>com.jee.mavenapp</groupId>
  <artifactId>hibernate-example</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>hibernate-example</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <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>4.3.10.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate.common</groupId>
        <artifactId>hibernate-commons-annotations</artifactId>
        <version>4.0.5.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.1-api</artifactId>
        <version>1.0.0.Final</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.12</version>
    </dependency>
  </dependencies>

2 Answers2

0

Add a forward slash in front of the xml file.

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

You can also omit the argument, because that is the default parameter, e.g.

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

This is what you need to do to load files that are on your classpath.

durron597
  • 31,968
  • 17
  • 99
  • 158
  • Thanks for the reply. I have already tested those options but still getting the same error. Any other suggestions? – Tuncer Tunçer Jul 25 '15 at 01:08
  • @TuncerTunçer `/src/main/resources` should be a resources folder not a source folder. Can you inclulde relevant pom snippets? – durron597 Jul 25 '15 at 01:15
0

It is not a fix of the problem, but can you put the hibernate.cfg.xml file in the same package as your Java class that builds session factory (to ensure that it in your classpath). And do something like this

Configuration  configuration = new Configuration().configure("/com/github/fluent/hibernate/example/mysql/hibernate.cfg.xml");
return  configuration.buildSessionFactory();

Replace "/com/github/fluent/hibernate/example/mysql/" with a package of your class.

P.S. You can find a very simply console Hibernate application here fluent-hibernate-mysql

v.ladynev
  • 19,275
  • 8
  • 46
  • 67