[Java]Maven項目打包導致的org.hibernate.boot.MappingNotFoundException: Mapping (RESOURCE) not found異常

異常類型

Exception in thread "main" org.hibernate.boot.MappingNotFoundException: Mapping (RESOURCE) not found : pers/model/domain/1tudent.hbm.xml : origin(pers/model/domain/1tudent.hbm.xml)
	at org.hibernate.boot.spi.XmlMappingBinderAccess.bind(XmlMappingBinderAccess.java:56)
	at org.hibernate.boot.MetadataSources.addResource(MetadataSources.java:294)
	at org.hibernate.boot.cfgxml.spi.MappingReference.apply(MappingReference.java:70)
	at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:468)
	at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:84)
	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:689)
	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)
	at Main.main(Main.java:14)

基本情況

  • 在確保hibernate.cfg.xml中映射文件配置無誤的情況下,仍舊報出這個異常。
<mapping resource="pers/model/domain/Student.hbm.xml"/>

我的項目結構爲:

在這裏插入圖片描述

異常分析

顯然這是一個尋址錯誤問題,尋找Maven項目編譯後的生成文件夾target

在這裏插入圖片描述
果然Maven在打包時未將配置文件Student.hbm.xml打包進去。

異常解決

檢查pom.xml文件,手動控制打包的細粒度:

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
        </plugins>
        <!-- 添加對 *.hbm.xml 的打包掃描-->
        <resources>
            <resource>
                <!--細粒度控制-->
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*hbm.xml</include>
                </includes>
            </resource>
        </resources>
    </build>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章