hibernate报错:org.hibernate.boot.MappingNotFoundException: Mapping (RESOURCE) not found

本文介绍了 Hibernate 中因映射配置文件路径设置错误导致的 MappingNotFoundException 异常,并给出了具体的解决办法。

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

org.hibernate.boot.MappingNotFoundException: Mapping (RESOURCE) not found : entity.Customer.hbm.xml : origin(entity.Customer.hbm.xml)
    at org.hibernate.boot.spi.XmlMappingBinderAccess.bind(XmlMappingBinderAccess.java:56)
    at org.hibernate.boot.MetadataSources.addResource(MetadataSources.java:275)
    at org.hibernate.boot.cfgxml.spi.MappingReference.apply(MappingReference.java:70)
    at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:469)
    at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:85)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:689)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)
    at hibernate.Dome1.test1(Dome1.java:38)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

这个错误可能是因为配置文件hibernate.cfg.xml里mapping写错了
我原来的mapping是这样的

<!-- 映射配置文件的位置 -->
        <mapping resource="entity.Customer.hbm.xml" />

百度一波之后发现是地址错误,正确的写法为

<mapping resource="entity/Customer.hbm.xml" />

就是把 . 变为 /

### Hibernate Core 5.4.9.Final 下载和使用说明 #### 下载方法 为了获取 Hibernate Core 5.4.9.Final 的源码与 jar 包,可以按照以下方式操作: 1. **访问官方网站** 打开 Hibernate 官方网站 [http://hibernate.org](http://hibernate.org),这是最权威的资源获取渠道[^1]。 2. **定位下载页面** 在首页导航栏中找到并点击 “More”,随后进入 Downloads 页面。这里会显示最新的稳定版本以及历史版本链接。对于特定版本(如 5.4.9.Final),可以选择左侧菜单中的 “Downloads” 部分来浏览所有可用的历史发布版本列表[^3]。 3. **选择目标版本** 浏览至对应版本号的位置,通常会有清晰标注的标签指向该版本的具体文件集合。点击对应的下载按钮即可开始传输过程。注意观察浏览器状态栏或者弹窗提示以确认下载成功与否[^1]。 4. **存储路径设置** 当文件被触发下载时,默认询问用户希望将其存放在本地计算机上的哪个目录下;建议指定易于管理且便于后续项目集成的固定位置作为默认保存地点。 #### 使用配置指导 完成上述步骤后得到所需的核心库及其附属文档等内容,则需进一步了解其基本应用流程如下所示: 1. **引入依赖项** 如果采用 Maven 构建工具的话,在 `pom.xml` 文件里加入下面这段声明语句用于自动拉取所需的构件: ```xml <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.4.9.Final</version> </dependency> ``` 2. **创建核心配置文件** 编辑名为 `hibernate.cfg.xml` 的 XML 格式的全局设定档,定义诸如连接池参数、SQL 方言类型等基础属性。例如针对 Google Cloud Spanner 数据库存储引擎的支持案例展示如下结构片段[^4]: ```xml <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="connection.driver_class">com.google.cloud.spanner.jdbc.JdbcDriver</property> <property name="connection.url">jdbc:cloudspanner:/projects/<project-id>/instances/<instance-id>/databases/<database-id></property> <!-- SQL dialect configuration --> <property name="dialect">org.hibernate.dialect.CloudSpannerDialect</property> <!-- Enable second-level cache --> <property name="cache.use_second_level_cache">true</property> <property name="cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property> <!-- Mapping annotated entity classes --> <mapping class="demo.Album"/> <mapping class="demo.Singer"/> </session-factory> </hibernate-configuration> ``` 3. **初始化 Session Factory 实例** 利用加载后的配置数据实例化一个持久层框架入口对象 ——SessionFactory ,并通过它派生出具体事务处理单元Session 。以下是典型实现代码样例: ```java import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.boot.MetadataSources; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.service.ServiceRegistry; public class HibernateUtil { private static final SessionFactory sessionFactory = buildSessionFactory(); private static SessionFactory buildSessionFactory() { try { ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() .configure("hibernate.cfg.xml") // Load global configurations from file. .build(); MetadataSources metadataSources = new MetadataSources(serviceRegistry); return metadataSources.buildMetadata().buildSessionFactory(); } catch (Throwable ex) { System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } } public static Session getSession(){ return sessionFactory.openSession(); } public void shutdown() { // Close caches and connection pools getSessionFactory().close(); } } ``` 4. **常见问题排查** - 若遇到异常信息类似于 `"Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set"`,则表明尚未正确定义所使用的数据库方言选项,请参照前述示例补充完整相关字段值[^2]。 --- ###
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值