[英]where does …hbm.xml file go?
我是一個Hibernate新手,嘗試使用嵌入式Derby數據庫的小型hibernate示例。 我在日食中發展。 我沒有使用Spring或Maven,我沒有設置Web應用程序,我沒有應用程序服務器。 如果項目變得更大,我將毫無疑問地使用其中的一些,但是現在我只是想讓這個例子起作用。
我得到的錯誤是:
Exception in thread "main" org.hibernate.MappingNotFoundException: resource: javabeat/net/hibernate/EmployeeInfo.hbm.xml not found
有時只是:
Exception in thread "main" org.hibernate.MappingNotFoundException: resource: EmployeeInfo.hbm.xml not found
這是我的代碼; 我已經標記了錯誤似乎來自哪里 - 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();
}
}
我在eclipse中的文件夾設置如下
CarRepair
--src
----javabeat
------net
--------hibernate
----main
------resources
--------javabeat
----------net
------------hibernate
我有一個EmployeeInfo.hbm.xml,我把它放在以下位置:src / javabeat / net / hibernate main / resources / javabeat / net / hibernate main / resources
我總是得到上述例外。 首先,它只是說它找不到XML文件; 在后兩者中,它在錯誤消息中的XML文件名前面加上javabeat / net / hibernate。
該文件應該在其他地方,還是我應該做的其他事情?
編輯:它可能是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>
你有一個特殊的目錄布局。 假設src
是Eclipse中的源文件夾,它會將所有非Java文件復制到classes或bin目錄(或者您為編譯類選擇的任何目錄名),而EmployeeInfo.hbm.xml
應該直接在src
下,因為你告訴Hibernate從類路徑的根目錄加載它:
cfg.addResource("EmployeeInfo.hbm.xml");
如果將它放在main / resources中,則應該加載它的代碼
cfg.addResource("main/resources/EmployeeInfo.hbm.xml");
為什么不使用自己的包層次結構,因此使用以下目錄樹:
src
com
rcook
myapp
正如您所說,您沒有使用maven,src / main / resources就像Eclipse項目的任何其他文件夾一樣。 因此,只需復制src文件夾下的hbm文件並刪除“addClass”方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.