Where is ... hbm.xml file go?

I am new to Hibernate having tried a little hibernate example with embedded Derby database. I am developing in an eclipse. I am not using Spring or Maven, I am not setting up a web application, I do not have an application server. I will no doubt use some of them if the project gets bigger, but for now I'm just trying to get this example to work.

The error I am getting:

Exception in thread "main" org.hibernate.MappingNotFoundException: resource: javabeat/net/hibernate/EmployeeInfo.hbm.xml not found

      

and sometimes just:

Exception in thread "main" org.hibernate.MappingNotFoundException: resource: EmployeeInfo.hbm.xml not found

      

Here is my code; I noted where the error appears: the eclipse console shows an exception there and stops working, which is a logical place:

package javabeat.net.hibernate;

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

public class JavaBeatHibernateExample
{
  public static void main(String args[]) throws Exception
  {

    configureDerbyEmbedded();

    Configuration cfg = new Configuration();
    cfg.addClass(javabeat.net.hibernate.EmployeeInfo.class);

    cfg.setProperty("hibernate.connection.driver_class", "org.apache.derby.jdbc.EmbeddedDriver");
    cfg.setProperty("hibernate.connection.password", "password");
    cfg.setProperty("hibernate.connection.url", "jdbc:derby:myEmbeddedDB;create=true");
    cfg.setProperty("hibernate.connection.username", "admin");
    cfg.setProperty("hibernate.dialect", "org.hibernate.dialect.DerbyDialect");
    cfg.setProperty("cache.provider_class", "org.hibernate.cache.NoCacheProvider");

    // Exception almost certainly generated here.
    cfg.addResource("EmployeeInfo.hbm.xml");

    cfg.setProperty("hibernate.current_session_context_class", "thread");
    cfg.setProperty("hibernate.show_sql", "true");
    SessionFactory sessionFactory = cfg.buildSessionFactory();
    Session session = sessionFactory.openSession();
    Transaction transaction = session.beginTransaction();
    transaction.begin();
    EmployeeInfo employeeInfo = new EmployeeInfo();
    employeeInfo.setSno(1);
    employeeInfo.setName("KamalHasan");
    session.save(employeeInfo);
    transaction.commit();
    session.close();
  }

  private static void configureDerbyEmbedded() 
      throws ClassNotFoundException, IllegalAccessException, InstantiationException
  {
    Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
  }
}

      

I have folders in eclipse configured as follows

CarRepair
--src
----javabeat
------net
--------hibernate
----main
------resources
--------javabeat
----------net
------------hibernate

      

I have EmployeeInfo.hbm.xml and I have placed it in the following locations: src / javabeat / network / hibernate home / resources / javabeat / network / hibernate home / resources

And I always get the above exception. First, it simply says that it cannot find the XML file; in the last two, it adds javabeat / net / hibernate before the XML filename in the error message.

Is the file supposed to be somewhere else, or is there something else I should be doing?

EDIT: Could it be in the xml file itself with the error message?

    <?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>
        <class name="javabeat.net.hibernate.EmployeeInfo" table="Employee_Info">
            <id name="sno" column="sno" type="java.lang.Integer">
            </id>
            <property name="name" column="name" type="java.lang.String"/>
        </class>
    </hibernate-mapping>

      

+3


source to share


2 answers


You have a pretty special directory layout. Assuming that src

is the source folder in Eclipse, it will copy all non-Java files to the classes or bin directory (or whatever directory name you choose for the compiled classes), and EmployeeInfo.hbm.xml

should be under src

, as you tell Hibernate load it from the root of the classpath:

cfg.addResource("EmployeeInfo.hbm.xml");

      

If you put it in main / resources, the code to load it should be



cfg.addResource("main/resources/EmployeeInfo.hbm.xml");

      

Why don't you use your own package hierarchy and therefore use the following directory tree:

src
  com
    rcook
      myapp

      

+2


source


As you said you are not using maven, src / main / resources is like any other folder for an Eclipse project. So just copy the hbm file to the src folder and remove the addClass method.



+1


source







All Articles