[英]where does …hbm.xml file go?
I am a Hibernate newbie, attempting a small hibernate example with an embedded Derby database. 我是一个Hibernate新手,尝试使用嵌入式Derby数据库的小型hibernate示例。 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. 我没有使用Spring或Maven,我没有设置Web应用程序,我没有应用程序服务器。 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: 我已经标记了错误似乎来自哪里 - eclipse控制台在那里显示异常并停止运行,这是合乎逻辑的地方:
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 我在eclipse中的文件夹设置如下
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 我有一个EmployeeInfo.hbm.xml,我把它放在以下位置: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; 首先,它只是说它找不到XML文件; in the latter two, it prepends javabeat/net/hibernate in front of the XML filename in the error message. 在后两者中,它在错误消息中的XML文件名前面加上javabeat / net / hibernate。
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文件本身的东西,带有误导性的错误信息?
<?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>
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: 假设src
是Eclipse中的源文件夹,它会将所有非Java文件复制到classes或bin目录(或者您为编译类选择的任何目录名),而EmployeeInfo.hbm.xml
应该直接在src
下,因为你告诉Hibernate从类路径的根目录加载它:
cfg.addResource("EmployeeInfo.hbm.xml");
If you place it in main/resources, the code to load it should be 如果将它放在main / resources中,则应该加载它的代码
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
As you said you are not using maven, src/main/resources is like any other folder for Eclipse project. 正如您所说,您没有使用maven,src / main / resources就像Eclipse项目的任何其他文件夹一样。 Hence just copy hbm file under src folder and remove the "addClass" method. 因此,只需复制src文件夹下的hbm文件并删除“addClass”方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.