Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate: Mapping not found exception

Tags:

java

hibernate

Started Hibernate from its official docs. I am stuck at the most basic step of configuring the xml files. Although I have done everything as described in the examples but it fails to find the The mapping file.

This is the exception:

Exception in thread "main" org.hibernate.MappingNotFoundException: resource: testing/ground/beans/User.hbm.xml not found
    at org.hibernate.cfg.Configuration.addResource(Configuration.java:728)
    at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2115)
    at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2087)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2067)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2020)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1935)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1914)
    at testing.ground.test.HibernateTest.main(HibernateTest.java:14)

The code with main():

package testing.ground.test;

import java.util.List;

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

    public class HibernateTest {
        private SessionFactory sessionFactory;

        public static void main(String[] args) {
            HibernateTest ht = new HibernateTest();
            ht.sessionFactory = new Configuration().configure().buildSessionFactory();
            ht.getData();
        }

        public void getData(){
            Session session = this.sessionFactory.getCurrentSession();
            session.beginTransaction();
            List list = session.createQuery("from users").list();
            System.out.println(list);
            session.getTransaction().commit();
            session.close();
        }



    }

The Hibernate config file:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/somedb?characterEncoding=UTF-8</property>
        <property name="connection.username">username</property>
        <property name="connection.password"/>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <mapping resource="testing/ground/beans/User.hbm.xml"/>

    </session-factory>

</hibernate-configuration>

I have tried to keep the folder structure same as that in the Hiberante examples:

Screen for package structure

Please advice.

like image 979
Mono Jamoon Avatar asked Dec 03 '22 22:12

Mono Jamoon


2 Answers

place user.hbm.xml file in resource folder under testing.ground.beans package

like image 110
Sagar Avatar answered Dec 28 '22 08:12

Sagar


It looks like you're using Maven to build your project. Maven expects resources (i.e. non Java files) to be under src/main/resources. Not undr src/main/java.

That said, find another tutorial that teaches how to map entities using annotations. XML mapping is obsolete, and much harder to use than annotations. As an addd bonus, you'll also learn how to use EclipseLink and all the other JPA engines, because annotations are standard, whereas XML mapping is not.

like image 43
JB Nizet Avatar answered Dec 28 '22 09:12

JB Nizet