2013-02-28 64 views
0

考慮下面的語句:Hibernate Eclipse WebProject配置:如何指定文件配置?

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

其中:

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

如何指定從中Hibernate會檢索配置數據的文件? 解釋,我有一個正確的配置數據的hibernate.cfg.xml文件中,但在運行時休眠拋出錯誤指的配置從另一個項目,如:

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

不管怎樣,我猜,默認配置正在添加從另一個文件。但是我搜索了我的hbm.xml文件,類和引用,並且它似乎沒問題。

關於這裏會發生什麼的任何想法?

Eclipse:Indigo SR2;休眠工具:3.5.1

謝謝!

回答

0

我回答我自己的問題,因爲我發現我的我認爲這是另一個問題的問題:

由於我的應用程序是基於Web的,因此Tomcat的lib文件夾中有一個來自另一個具有休眠配置問題的項目的.jar文件。 Hibernate檢查與某個項目相關的所有依賴項(包括所有.jar),其中包括Tomcat的lib中的所有jar。所以,在運行時出現異常(當時對我完全陌生)。解決.jar文件的休眠配置問題或從Tomcat的lib文件夾中刪除它們(如果不是必需的話)解決了這個問題。

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); 
     } 
    } 

最好的問候:)

+1

謝謝你的迴應(我投了票)。我發現我的問題,正如我上面的回答。 – Alex 2013-03-01 14:01:41

+0

我很高興你能找到解決你的問題,最好的問候。 – 2013-03-02 16:00:27