0

考虑以下语句:

SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();

在哪里:

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

如何指定 Hibernate 将从中检索配置数据的文件?解释一下,我有一个带有正确配置数据的 hibernate.cfg.xml,但是在运行时 hibernate 会抛出引用来自其他项目的配置的错误,例如:

org.hibernate.MappingNotFoundException: resource: xx/AnotherNonRelatedProject/CertainClass.hbm.xml not found

无论如何,我猜默认配置来自另一个文件。但我已经搜索了我的 hbm.xml 文件、类和引用,似乎没问题。

知道这里会发生什么吗?

Eclipse:靛蓝 SR2;休眠工具:3.5.1

谢谢!

4

2 回答 2

1

如果要设置自己的 Hibernate 配置,则必须使用以下内容:

private static final SessionFactory sessionFactory;
    static {
        try {
            // Create your SessionFactory from hibernate.cfg.xml
            sessionFactory = new Configuration().configure(new File("hibernate1.cfg.xml"))
                    .buildSessionFactory();
        } catch (Throwable ex) {
            throw new ExceptionInInitializerError(ex);
        }
    }

最好的祝福 :)

于 2013-02-28T13:30:53.670 回答
0

我正在回答我自己的问题,因为我发现了我的具体问题,我认为这将是另一个问题的问题:

由于我的应用程序是基于 Web 的,因此 Tomcat 的 lib 文件夹中有一个来自另一个项目的 .jar 文件,该文件存在休眠配置问题。由于 Hibernate 检查与某个项目相关的所有依赖项(包括所有 .jar),其中包括 Tomcat 库中的所有 jar。因此,在运行时出现了异常(当时对我来说完全陌生)。解决 .jar 文件的休眠配置问题或从 Tomcat 的 lib 文件夹中删除它们(如果没有必要)可以解决问题。

于 2013-03-01T14:00:05.717 回答