4

我是一个 Hibernate 新手,正在尝试一个带有嵌入式 Derby 数据库的小型休眠示例。我正在eclipse中开发。我没有使用 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>
4

2 回答 2

2

您有一个非常特殊的目录布局。假设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
于 2013-02-03T15:27:34.927 回答
1

正如您所说,您没有使用 maven,src/main/resources 就像 Eclipse 项目的任何其他文件夹一样。因此只需复制 src 文件夹下的 hbm 文件并删除“addClass”方法。

于 2013-02-03T15:41:23.363 回答