David J.
David J.

Reputation: 35

Mapping (RESOURCE) not found

Guys I am new with Hibernate and I am struggling to make a hello world app work. Below I will post the exception I am getting as well as my code - with the hope maybe someone can spot other errors which I have and save me from "future" troubles.

Code:

package org.arpit.javapostsforlearning;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity(name = "User_table")
public class User {

    @Id
    int userId;

    @Column(name = "User_Name")
    String userName;

    String userMessage;

    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;
    }

    public String getUserMessage() {
        return userMessage;
    }

    public void setUserMessage(String userMessage) {
        this.userMessage = userMessage;
    }

}

This:

package org.arpit.javapostsforlearning;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration; 
import org.hibernate.service.ServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;

public class HibernateMain {
    public static void main(String[] args)
    {
        Configuration configuration=new Configuration();
        configuration.configure(); 
        configuration.addClass(org.arpit.javapostsforlearning.User.class);

        ServiceRegistry sr= new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build(); 
        SessionFactory sf=configuration.buildSessionFactory(sr);

        User user1=new User(); 
        user1.setUserName("Arpit");
        user1.setUserMessage("Hello world from arpit");

        User user2=new User();
        user2.setUserName("Ankita");
        user2.setUserMessage("Hello world from ankita");

        Session ss=sf.openSession();

        ss.beginTransaction(); //saving objects to session
        ss.save(user1);
        ss.save(user2);
        ss.getTransaction().commit();
        ss.close(); 

    }

}

And XMLS:

<?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.microsoft.sqlserver.jdbc.SQLServerDriver</property>
        <property name="connection.url">jdbc:sqlserver://localhost:1433;database=Sandboxlearning;integratedSecurity=true</property>


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

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.SQLServer2005Dialect</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 class="User.hbm.xml"/> 

    </session-factory>

</hibernate-configuration>

User.hbm.xml

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

<hibernate-mapping>
   <class name="User" table="User_table">
      <meta attribute="class-description">
         This class contains the user detail. 
      </meta>
      <id name="userId" type="int" column="userId">
         <generator class="native"/>
      </id>
      <property name="userName" column="User_Name" type="string"/>
      <property name="userMessage" column="userMessage" type="string"/>
   </class>
</hibernate-mapping>

Exception:

Jun 28, 2016 7:17:15 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.0.Final}
Jun 28, 2016 7:17:15 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Jun 28, 2016 7:17:15 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Jun 28, 2016 7:17:15 PM org.hibernate.boot.jaxb.internal.stax.LocalXmlResourceResolver resolveEntity
WARN: HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-configuration. Use namespace http://www.hibernate.org/dtd/hibernate-configuration instead.  Support for obsolete DTD/XSD namespaces may be removed at any time.
Exception in thread "main" org.hibernate.boot.MappingNotFoundException: Mapping (RESOURCE) not found : org/arpit/javapostsforlearning/User.hbm.xml : origin(org/arpit/javapostsforlearning/User.hbm.xml)
    at org.hibernate.boot.spi.XmlMappingBinderAccess.bind(XmlMappingBinderAccess.java:56)
    at org.hibernate.boot.MetadataSources.addResource(MetadataSources.java:274)
    at org.hibernate.boot.MetadataSources.addClass(MetadataSources.java:262)
    at org.hibernate.cfg.Configuration.addClass(Configuration.java:513)
    at org.arpit.javapostsforlearning.HibernateMain.main(HibernateMain.java:14)

The XMLS are stored under src folder.

Here is also screen shot of my App

image

Upvotes: 1

Views: 6344

Answers (1)

Hrabosch
Hrabosch

Reputation: 1583

Take a look to: <mapping class="User.hbm.xml"/>. But you using resource. You are able to use class when you saying that map me class for that, but you want to map resource. So just use <mapping resource="User.hbm.xml"/>

Upvotes: 1

Related Questions