[Java]Maven项目打包导致的org.hibernate.boot.MappingNotFoundException: Mapping (RESOURCE) not found异常

在Java Maven项目中,遇到org.hibernate.boot.MappingNotFoundException异常,即使映射文件配置正确,打包后仍无法找到。问题根源在于Maven打包时未包含配置文件。通过深入分析,确认为寻址错误,解决办法是手动调整Maven打包设置,确保映射资源被正确包含。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

异常类型

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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值