Question

[Solved] hibernate.properties does not found in maven project using hibernate

I tried many ways to solve this error (using maven project)

  1. I put hibernate.cfg.xml file in src folder then got same error(hibernate.properties does not found)
  2. I put hibernate.cfg.xml file in resource folder then got same error(hibernate.properties does not found)
  3. I put hibernate.cfg.xml file in webapp/WEB-INF folder then got same error(hibernate.properties does not found)
  4. All properties of database i have written in hibernate.cfg.xml is as belows
<?xml version='1.0' encoding='UTF-8'?>  
<!DOCTYPE hibernate-configuration PUBLIC  
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
 
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
        <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/testdb</property>
        <property name="hibernate.connection.username">postgres</property>
        <property name="hibernate.connection.password">sql123</property>
        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
        <property name="hbm2ddl.auto">create </property>
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        <mapping resource="Employee-hbn.xml" />
    </session-factory>
</hibernate-configuration>

I got exception is as follows:

May 22, 2018 2:28:38 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.3.0.Final}
May 22, 2018 2:28:38 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Could not locate cfg.xml resource [/src/main/java/hibernate.cfg.xml]
error

If you have solution for this then tell me

Thanks in advance….

Solution #1:

Could not locate cfg.xml resource [/src/main/java/hibernate.cfg.xml]
error

This means that Hibernate tries to find hibernate.cfg.xml in the

/src/main/java/hibernate.cfg.xml.

This is an incorrect path.

You should:

  1. Put hibernate.cfg.xml in the resources folder.
  2. Use Configuration configuration = new Configuration().configure() for configuration.
  3. Refresh project and check that hibernate.cfg.xml resides in the root of the build folder.

Some explanations:

Configuration configuration = new Configuration().configure()

means the same as

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

And this line tells Hibernate to locate hibernate.cfg.xml in the root of the class path. For example, if you build a jar this file should be in the root of the jar.

For a war the root of the class path is WEB-INF/classes.

When you put files in the resources folder Maven puts all content of the resources folder to the root of the class path (to the root of jar, or to the ). So you will not have the resources folder in the jar, just content of it.

Your IDE do the same when you refresh the project.

Respondent: Jyoti Jadhav

Solution #2:

Your hibernate.cfg.xml needs to be visible in the classpath.

For a web placing it in WEB-INF/classes will probably work.

For a maven project which is not web you can use /src/main/resources/ which is the default maven resources folder (or use a different one if you have switched it).

Also in your pom.xml you need to have a dependency to hibernate-core

Respondent: v.ladynev

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy