Reputation: 13123
I am a Hibernate newbie, attempting a small hibernate example with an embedded Derby database. I am developing in eclipse. I am not using Spring or Maven, I am not setting up a web application, I have no application server. I will no doubt use some of those if the project gets bigger, but right now I'm just trying to get this example to work.
The error I am getting is:
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 have marked where the error appears to be coming from - eclipse console shows the exception there and stops running, and it's the 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 the folders in eclipse set up as follows
CarRepair
--src
----javabeat
------net
--------hibernate
----main
------resources
--------javabeat
----------net
------------hibernate
I have an EmployeeInfo.hbm.xml, and I have put it in the following places: src/javabeat/net/hibernate main/resources/javabeat/net/hibernate main/resources
And I always get the above exception. In the first, it just says it cannot find the XML file; in the latter two, it prepends javabeat/net/hibernate in front of the XML filename in the error message.
Is the file supposed to be somewhere else, or is there something else I'm supposed to be doing?
EDIT: Could it be something in the xml file itself, with a misleading 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>
Upvotes: 4
Views: 7761
Reputation: 3173
As you said you are not using maven, src/main/resources is like any other folder for Eclipse project. Hence just copy hbm file under src folder and remove the "addClass" method.
Upvotes: 1
Reputation: 691735
You have quite a special directory layout. Assuming src
is a source folder in Eclipse, it will copy all the non-Java files to the classes or bin directory (or whatever directory name you chose for the compiled classes), and the EmployeeInfo.hbm.xml
should be directly under src
, since you're telling Hibernate to load it from the root of the classpath:
cfg.addResource("EmployeeInfo.hbm.xml");
If you place 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 thus use the following directory tree:
src
com
rcook
myapp
Upvotes: 2