2014-04-03 38 views
1

不,這不是this onethis onethis one同樣的問題。org.hibernate.MappingNotFoundException:資源:COM/CORP /部門/凸出/ FooTbl.hbm.xml沒有找到

原因很簡單,我的* .hbm.xml文件已經在正確的目錄(src/main/resources/com/corp/dept/proj)中。

此外,使用一切正常,只要我靜態(即在編譯的時候硬編碼)採取一切從hibernate.cfg.xml,用我的HibernateUtil.java這種簡單的靜態方法:

private static SessionFactory buildSessionFactory() { 
    try { 
     return new Configuration().configure("resources/hibernate.cfg.xml").buildSessionFactory(); 
    } 
    catch (Throwable ex) { 
     System.err.println("Initial SessionFactory creation failed." + ex); 
     throw new ExceptionInInitializerError(ex); 
    } 
} 

但因爲我想移動的一些參數從hibernate.cfg.xml到屬性文件在運行時被讀取(例如,DB的「connection.url」):

static { 
    try { 
    sessionFactory = buildHibernateConfig().buildSessionFactory(); 
    } catch (Throwable ex) { 
    System.out.println("Initial SessionFactory creation failed: " + ex.getMessage()); 
    throw new ExceptionInInitializerError(ex); 
    } 
} 

private static Configuration buildHibernateConfig() throws FileNotFoundException, IOException { 

    Properties properties = new Properties(); 
    sConfig = new Configuration().configure("resources/hibernate.cfg.xml"); 

    try { 
    properties.load(ServiceUtils.class.getResourceAsStream(MYWS_PROPS)); 
    } 
    catch (IOException e) { 
    LOG.severe("Cannot find properties file " + MYWS_PROPS); 
    throw new SecurityException("Server Error: couldn't open " + MYWS_PROPS + " file"); 
    } 

    /*hardcoded*/sConfig.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");/*hardcoded*/ 
    /*hardcoded*/sConfig.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");/*hardcoded*/ 
    sConfig.setProperty("hibernate.connection.url", properties.getProperty("dburl")); 

    // Add Hibernate XML mappings 
    sConfig.addClass(FooTbl.class); 

    return sConfig; 
} 

我得到一個運行例外:

Apr 03, 2014 14:31:49 AM org.hibernate.annotations.common.Version <clinit> 
INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final} 
Apr 03, 2014 14:31:49 AM org.hibernate.Version logVersion 
INFO: HHH000412: Hibernate Core {4.2.6.Final} 
Apr 03, 2014 14:31:49 AM org.hibernate.cfg.Environment <clinit> 
INFO: HHH000206: hibernate.properties not found 
Apr 03, 2014 14:31:49 AM org.hibernate.cfg.Environment buildBytecodeProvider 
INFO: HHH000021: Bytecode provider name : javassist 
Apr 03, 2014 14:31:49 AM org.hibernate.cfg.Configuration configure 
INFO: HHH000043: Configuring from resource: resources/hibernate.cfg.xml 
Apr 03, 2014 14:31:49 AM org.hibernate.cfg.Configuration getConfigurationInputStream 
INFO: HHH000040: Configuration resource: resources/hibernate.cfg.xml 
Apr 03, 2014 14:31:49 AM org.hibernate.cfg.Configuration addResource 
INFO: HHH000221: Reading mappings from resource: resources/com/corp/dept/proj/FooTbl.hbm.xml 

Apr 03, 2014 14:31:49 AM org.hibernate.cfg.Configuration doConfigure 
INFO: HHH000041: Configured SessionFactory: null 
Apr 03, 2014 14:31:49 AM org.hibernate.cfg.Configuration addClass 
INFO: HHH000221: Reading mappings from resource: com/corp/dept/proj/FooTbl.hbm.xml 
Apr 03, 2014 14:31:49 AM org.hibernate.cfg.Configuration addResource 
INFO: HHH000221: Reading mappings from resource: com/corp/dept/proj/FooTbl.hbm.xml 
Initial SessionFactory creation failed: resource: com/corp/dept/proj/FooTbl.hbm.xml not found 

Caused by: org.hibernate.MappingNotFoundException: resource: com/corp/dept/proj/FooTbl.hbm.xml not found 
     at org.hibernate.cfg.Configuration.addResource(Configuration.java:712) 
     at org.hibernate.cfg.Configuration.addClass(Configuration.java:757) 
     at com.corp.dept.proj.myws.HibernateUtil.buildHibernateConfig(HibernateUtil.java:141) 
     at com.corp.dept.proj.myws.HibernateUtil.<clinit>(HibernateUtil.java:56) 

現在...例外之前,日誌顯示文件是發現。我還驗證了這些文件實際上包含在WAR文件中的預期相同的子目錄中。所以,爲什麼是這個錯誤?

什麼是解決這個問題的建議方法?

+0

嘗試一個測試並將FooTbl.hbm.xml複製到類文件夾,與您的.class文件位於相同 – fmodos

+0

我注意到在您的日誌文件中,當它找到該文件時,它正在讀取' resources/com/corp/dept/proj/FooTbl.hbm.xml',當它沒有嘗試讀取'com/corp/dept/proj/FooTbl.hbm.xml'。 – azurefrog

回答

0

我找到了正確的解決方案:Configuration.configure()之間

,而不是去的靜態初始化塊路線,只是堅持與原來靜態buildSessionFactory()方法並注入Configuration.setProperty()語句(如需要)在Configuration.BuildSessionFactory()。也就是說,代替了原來的執行buildSessionFactory的()被張貼在的問題,我做的:

private static SessionFactory buildSessionFactory() { 
    try { 
    Configuration config = new Configuration(); 
    config = config.configure("resources/hibernate.cfg.xml"); 
    config.setProperty("hibernate.connection.url", properties.getProperty("dburl"));    
    SessionFactory session = config.buildSessionFactory(); 
    return session; 
    } 
    catch (Throwable ex) { 
    System.err.println("Initial SessionFactory creation failed." + ex); 
    throw new ExceptionInInitializerError(ex); 
    } 
} 

無需擴展Configuration類和...奇蹟般有效。 :)

1

獲得配置(sConfig)實例後,您嘗試錯誤地添加XML映射文檔。由於您使用的是基於XML的映射文件(.hbm),所以到添加xml映射文件需要使用addResource()方法,而不是addClass()方法

addClass()方法用於添加基於註釋的映射。

因此,與

sConfig.addResource("com/corp/dept/proj/FooTbl.hbm.xml"); 

希望這能夠解決您的問題,請更換

sConfig.addClass(FooTbl.class); 

+0

感謝您的提示。我嘗試了你的建議,問題是一樣的。我也用'「resources/com/corp/dept/proj/FooTbl.hbm.xml」試過它,結果出現了另一個問題。它看起來像我將不得不放棄靜態初始化塊方法,而是保留原始實現,只是擴展'Configuration'類來覆蓋從'hibernate.cfg.xml'中讀取的內容。如果可行,我會將其作爲答案發布。提示+1。 – Withheld

0

請確保您的文件FooTbl.hbm.xml名稱應與addClass()方法中的類名匹配。

這裏你的類名是FooTbl,那麼映射文件名應該是FooTbl.hbm.xml否則會產生異常。

試試這個。 :)