【问题标题】:Hibernate Eclipse WebProject configuration: How to specify file config?Hibernate Eclipse WebProject 配置:如何指定文件配置?
【发布时间】:2013-02-14 16:17:25
【问题描述】:

考虑以下语句:

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

谢谢!

【问题讨论】:

    标签: eclipse hibernate


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      如果要设置自己的 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);
              }
          }
      

      最好的问候:)

      【讨论】:

      • 感谢您的回复(我放弃了投票)。正如我上面的回答,我发现了我的问题。
      • 我很高兴你能找到解决问题的方法,最好的问候。
      猜你喜欢
      • 2014-11-30
      • 2013-10-25
      • 2010-12-10
      • 2011-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-12
      相关资源
      最近更新 更多