vardius
vardius

Reputation: 6546

Why I get MappingNotFound Exception

Hello i get exception from mapping my hibernate: Initial SessionFactory creation failed.org.hibernate.MappingNotFoundException: resource: bbstats/domain/User.hbm.xml not found Exception in thread "main" java.lang.ExceptionInInitializerError

here is my hibernate.cfg.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>
        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://sql10.lh.pl:3306/zamocno_bb</property>
        <property name="connection.username">root</property>
        <property name="connection.password"></property>

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

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

        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

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

        <property name="hbm2ddl.auto">validate</property>

        <mapping resource="bbstats/domain/User.hbm.xml"/>


    </session-factory>
</hibernate-configuration>

here is my User.hbm.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="bbstats.domain">

    <class name="bbstats.domain.Users" table="USERS">
        <id name="id" column="ID">
            <generator class="native"/>
        </id>
        <property name="title" column="nick"/>
    </class>

</hibernate-mapping>

and here Users.class:

package bbstats.domain;

/**
 * Created with IntelliJ IDEA.
 * User: Rafał
 * Date: 25.03.13
 * Time: 13:33
 * To change this template use File | Settings | File Templates.
 */
public class Users
{
    private Long id;
    private String title;

    public Users() {}

    public Users(Long id, String title) {
        this.id = id;
        this.title = title;
    }

    public Long getId() {
        return id;
    }

    private void setId(Long id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}

and HibernateUtil.class:

package bbstats.util;

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

public class HibernateUtil
{

    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            return new Configuration()
                    .configure()
                    .buildSessionFactory();
        } catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

structure of catalogs: enter image description here

Upvotes: 1

Views: 2241

Answers (2)

Ananth Kumar
Ananth Kumar

Reputation: 21

Make sure in Eclipse is that the Java build path must reflect */.xml. Right click on project > Properties > Java Build Path > Source > Add */.xml (Source folder on build path,include **.xml)

Upvotes: 1

subodh
subodh

Reputation: 6158

Problem is in your User.hbm.xml

Here

<class name="bbstats.domain.Users" table="USERS">
        <id name="id" column="ID">
            <generator class="native"/>
        </id>
        <property name="title" column="nick"/>
    </class>

remove fully qualified name of your class because you already provided the package name of your class in this <hibernate-mapping package="bbstats.domain"> line

Update your User.hbm.xml with below code

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="bbstats.domain">

    <class name="Users" table="USERS">
        <id name="id" column="ID">
            <generator class="native"/>
        </id>
        <property name="title" column="nick"/>
    </class>

</hibernate-mapping>

Note: Your pojo name is Users.java I would suggest you to please rename your User.hbm.xml to Users.hbm.xml as per your domain object so that it is easy to read.

Upvotes: 1

Related Questions