3

I have been trying my hand to implement hibernate using a small example.

Below is my hibernate.config.xml

<?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="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/abc
</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="show_sql">true</property>
<mapping
resource="HibernateExample/src/HibernateExposed/Resource/Person.hbm.xml"/>
</session-factory>
</hibernate-configuration>

When running the code I am getting error org.hibernate.boot.MappingNotFoundException: Mapping (RESOURCE) not found.

I tried replacing mapping as below

<mapping
resource="Resource/Person.hbm.xml"/>

and also tried to keep the mapping xml at same location as hibernate.config.xml.

<mapping
resource="Person.hbm.xml"/>

Under none of above mentioned cases, code can find my Person.hbm.xml.

My folder structure looks as below

enter image description here

I looked at all other answers for this error on Stackoverflow but none of the approaches resolved this issue. Any help is highly appreciated. Also, is there any approach to debug this further to granular level?

rahulv21
  • 187
  • 2
  • 3
  • 15

2 Answers2

2

Please, put hibernate.cfg.xml in the root of src folder.

And use

<mapping resource="HibernateExposed/Person.hbm.xml"/>

Hibernate loads all those files using ClassLoader.getResourceAsStream(resourcePath) resourcePath — is the path to the file

ClassLoader tries to get access to the files in the root of bin or build folder in the IDE, or root of jar, or root of war/WEB-INF/classes/ for web-applications. Those all are the root of the class path.

bin is a folder where Eclipse compiles your files. The root of src folder is compiled to the root of bin folder. You can check it.

For an example

configure("hibernate.cfg.xml")bin/hibernate.cfg.xml configure("xxx/hibernate.cfg.xml")bin/xxx/hibernate.cfg.xml

 <mapping resource="HibernateExposed/Person.hbm.xml"/>

corresponds bin/HibernateExposed/Person.hbm.xml

A path should be without the leading / for a ClassLoader. Hibernate tries to delete the leading /.

A path like this is valid too

<mapping resource="/HibernateExposed/Person.hbm.xml"/>

Update

You can specify path to the hibernate.cfg.xml, if you don't want to have it in the root

new Configuration().configure("HibernateExposed/hibernate.cfg.xml")

if you use

new Configuration().configure()

it should be in the root of the class path.

v.ladynev
  • 19,275
  • 8
  • 46
  • 67
  • If I put hibernate.cfg.xml at the src folder, I get error org.hibernate.internal.util.config.ConfigurationException: Could not locate cfg.xml resource. Anyways, hibernate.cfg.xml should be present at the root of classpath right? In my case, the class(Person) is created under HibernateExposed(package), so its location should not be a problem. Correct me if I am wrong. – rahulv21 May 03 '16 at 14:18
  • After making changes to configure(hibernate.cfg.xml), it worked. I have been stuck with this problem for almost last 6 hours. Thanks for the explaination! – rahulv21 May 03 '16 at 14:30
  • @bestrahul21 I update. Check your build folder. Sometimes Eclipse doesn't clean it. You need to choose clean from a menu. – v.ladynev May 03 '16 at 14:32
  • @bestrahul21 Because Eclipse doesn't rebuild a project after you change a file location. – v.ladynev May 03 '16 at 14:32
0

none of this worked for me, then I tried this:-

(replace this method in FactoryConfiguration singleton Class)

private FactoryConfiguration() {
        Configuration configuration = new Configuration().configure("hibernate.cfg.xml").addAnnotatedClass(***Class Name***.class);
        sessionFactory = configuration.buildSessionFactory();
    }
Procrastinator
  • 2,526
  • 30
  • 27
  • 36