org.hibernate.internal.util.config.ConfigurationException: Unable to perform unmarshalling at line n

本文记录了一个因拼写错误导致的Hibernate配置问题。作者在配置Hibernate时,由于将资源文件名的属性名“resource”误拼为“resourse”,从而引发ConfigurationException异常。此错误导致XML配置文件无法被正确解析。
org.hibernate.internal.util.config.ConfigurationException: Unable to perform unmarshalling at line number -1 and column -1 in RESOURCE hibernate.cfg.xml. Message: cvc-complex-type.3.2.2: 元素 'mapping' 中不允许出现属性 'resourse'。
    at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:133)
    at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:65)
    at org.hibernate.boot.cfgxml.internal.ConfigLoader.loadConfigXmlResource(ConfigLoader.java:57)
    at org.hibernate.boot.registry.StandardServiceRegistryBuilder.configure(StandardServiceRegistryBuilder.java:163)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:258)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:244)
    at test.StudentsTest.init(StudentsTest.java:30)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    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)
Caused by: javax.xml.bind.UnmarshalException
 - with linked exception:
[org.xml.sax.SAXParseException; cvc-complex-type.3.2.2: 元素 'mapping' 中不允许出现属性 'resourse'。]
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.handleStreamException(Unknown Source)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(Unknown Source)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(Unknown Source)
    at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:126)
    ... 30 more
Caused by: org.xml.sax.SAXParseException; cvc-complex-type.3.2.2: 元素 'mapping' 中不允许出现属性 'resourse'。
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator$XSIErrorReporter.reportError(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.reportSchemaError(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.processAttributes(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.startElement(Unknown Source)
    at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorHandlerImpl.startElement(Unknown Source)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.ValidatingUnmarshaller.startElement(Unknown Source)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.InterningXmlVisitor.startElement(Unknown Source)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.StAXEventConnector.handleStartElement(Unknown Source)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.StAXEventConnector.bridge(Unknown Source)

    ... 33 more



解决问题:

太相信自己了,压根没有想到是自己resource拼写错了,还以为是自己的jar包错了,搞了好久。真是要命,英语差真是没谁了,写个博客记录自己做的蠢事。

   
在使用 Hibernate 框架时,`ConfigurationException: Could not locate cfg.xml resource [hibernate.cfg.xml]` 是一个常见的问题,通常与配置文件的路径配置不正确或资源未正确加载有关。以下是对该异常的分析与解决方案。 ### 异常原因分析 Hibernate 在初始化过程中会尝试加载 `hibernate.cfg.xml` 配置文件,如果该文件未在类路径中找到,或指定的路径无效,则会抛出 `ConfigurationException` 异常。该问题的核心原因包括: - 配置文件未放置在正确的类路径下(如 `src/main/resources`)。 - 手动指定的文件路径不正确,导致 Hibernate 无法定位文件。 - 构建项目时,配置文件未被正确包含在输出目录中(如 `target/classes`)。 ### 解决方案 #### 1. 确保配置文件位于类路径中 Hibernate 默认会从类路径中加载 `hibernate.cfg.xml` 文件。因此,应将该文件放置在项目的资源目录中(如 Maven 项目的 `src/main/resources`),确保构建时该文件会被复制到 `target/classes` 目录下。 #### 2. 显式指定配置文件路径 如果配置文件未放置在默认类路径下,可以通过 `configure(String resource)` 方法显式指定其路径。例如: ```java Configuration configuration = new Configuration().configure("com/learnning/hibernate/demo1/hibernate.cfg.xml"); ``` 此方式确保 Hibernate 从指定路径加载配置文件,避免因路径错误导致异常 [^3]。 #### 3. 检查构建配置 在使用构建工具(如 Maven 或 Gradle)时,需确认资源过滤配置是否正确,确保 `hibernate.cfg.xml` 被正确包含在构建输出中。例如,在 `pom.xml` 中添加以下配置以确保资源文件被正确处理: ```xml <build> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources> </build> ``` #### 4. 验证运行时类路径 在运行时环境中,确保 `hibernate.cfg.xml` 文件存在于类路径中。可以通过检查 `target/classes` 目录或生成的 JAR 包确认文件是否存在。 ### 总结 `ConfigurationException` 的核心问题是 Hibernate 无法找到 `hibernate.cfg.xml` 配置文件。通过确保文件位于类路径、显式指定路径、检查构建配置以及验证运行时类路径,可以有效解决此类异常。
评论 5
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值