3

I am trying to do a simple hibernate program. I am following the steps given in this tutorial.

The Error that I am getting is

   org.hibernate.MappingNotFoundException: resource: org.manu.dtd.UserDetails not found
    at org.hibernate.cfg.Configuration.addResource(Configuration.java:799)
    at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2344)
    at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2310)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2290)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2243)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:2158)
    at org.manu.dtd.TestHibernate.main(TestHibernate.java:16)

Here is my folder structure

enter image description here

My persitence class with annotations is

package org.manu.dtd;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class UserDetails {

    @Id
    private int userId;

    private String userName;
    public int getUserId() {
        return userId;
    }
    public void setUserId(int userId) {
        this.userId = userId;
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }

}

My hibernate.cfg.xml file is

<?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">oracle.jdbc.driver.OracleDriver</property>
        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:XE</property>
        <property name="connection.username">user</property>
        <property name="connection.password">password</property>
        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.Oracle10gDialect</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">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">create</property>

        <mapping resource="org.manu.dtd.UserDetails"/>

    </session-factory>

</hibernate-configuration>

And finally my main program is

package org.manu.dtd;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class TestHibernate {

    public static void main(String[] args) {
        UserDetails user = new UserDetails(); 
        user.setUserId(23);
        user.setUserName("Renu");
        System.out.println("setting values complete");
        try {
        SessionFactory sF = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
        Session session = sF.openSession();
        session.beginTransaction();
        session.save(user);
        session.getTransaction().commit();
        }
        catch (HibernateException he) {
            he.printStackTrace();
        }
    }

}

Can anyone please help me out to resolve this issue.

2 Answers 2

15

Well it looks it can be fixed even easier. You should use <mapping class=..> instead of <mapping resource=..> as resources is for mapping other xml files describing entities and such. Here is small example from the offical tutorials

Sign up to request clarification or add additional context in comments.

1 Comment

I applied the above fix and put this <mapping class"org.manu.dtd.UserDetails"/> in the hibernate.cfg.xml file and it worked like a charm thank you.
0

Provide fully qualified name for hibernate configuration mapping file,

SessionFactory sF = new Configuration().
            configure("/org/manu/dtd/hibernate.cfg.xml").buildSessionFactory();

1 Comment

When I put the path as mentioned above, the error is Exception in thread "main" org.hibernate.HibernateException: /org/manu/dtd/hibernate.cfg.xml not found

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.