当前位置: 首页 > 知识库问答 >
问题:

组织。冬眠InvalidMappingException:无法从资源com/lara/Person解析映射文档。哈佛商学院。xml

颜哲彦
2023-03-14
Exception in thread "main" org.hibernate.InvalidMappingException: Could not parse mapping document from resource com/lara/Person.hbm.xml
    at org.hibernate.cfg.Configuration.addResource(Configuration.java:616)
    at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1635)
    at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1603)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1582)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1556)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1476)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1462)
    at com.lara.Manager.main(Manager.java:15)
Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from invalid mapping
    at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:549)
    at org.hibernate.cfg.Configuration.addResource(Configuration.java:613)
    ... 7 more
Caused by: org.xml.sax.SAXParseException; lineNumber: 7; columnNumber: 39; Document root element "hibernate-mapping", must match DOCTYPE root "hibernate-configuration".
    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.dtd.XMLDTDValidator.rootElementSpecified(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.handleStartElement(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.startElement(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
    at org.dom4j.io.SAXReader.read(SAXReader.java:465)
    at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:546)
    ... 8 more
        <?xml version='1.0' encoding='utf-8'?>
       <!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
        <hibernate-configuration>

    <session-factory>

        <!-- Database connection settings -->
        <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:XE</property>
        <property name="connection.username">system</property>
        <property name="connection.password">system</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">2</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.OracleDialect</property>

        <!-- Enable Hibernate's current session context -->
        <property name="current_session_context_class">org.hibernate.context.ManagedSessionContext</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">create</property>


        <mapping resource="com/lara/Person.hbm.xml"/>

    </session-factory>

     </hibernate-configuration>




Person.hbm.xml
==============
        <?xml version='1.0' encoding='utf-8'?>
        <!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">


        <hibernate-mapping package="com.lara">

    <class name="Person" table="PERSON">
        <id name="id" column="PERSON_ID">
            <generator class="native" />
        </id>
        <property name="age"/>
        <property name="firstname"/>
        <property name="lastname"/>

    </class>

        </hibernate-mapping>


Person.java
=============
        package com.lara;

        public class Person {
    private int id;
    private String firstName;
    private String lastName;
    private int age;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

      }

Manager.java
=============
        package com.lara;

        import org.hibernate.Session;
        import org.hibernate.SessionFactory;
        import org.hibernate.cfg.Configuration;

        public class Manager {

    public static void main(String[] args) {
        Person p1 = new Person();
        p1.setFirstName("abc");
        p1.setLastName("xyz");
        p1.setAge(22);

        Configuration c1 = new Configuration().configure();

        SessionFactory sf = c1.buildSessionFactory();
        Session s1 = sf.openSession();

        s1.beginTransaction();
        s1.save(p1);
        s1.getTransaction().commit();

        s1.flush();
        s1.close();
        System.out.println("done");
     }
       }

共有3个答案

卞坚成
2023-03-14

InvalidmappingException在某些情况下会出现,请仔细阅读

>

  • 我们将使用id标签来代替我们使用的,那么这个异常肯定会发生。

    在Pojo类中,你在数据库中声明像"name"这样的数据成员,然后在映射文件中声明"name"ok,然后在映射文件中,我们可以简单地说,就像你在id标签名称属性值中声明的任何东西一样,它对应的pojo中没有数据成员类然后只有一个错误出现InvalidMappingExcture.

    当或此列名与相应的数据库存档不完全匹配时,出现SQLGrammarExc0019。

  • 桑成荫
    2023-03-14

    很简单

    1.create构造函数

    public Person(){}
    

    这是Hibernate中的主要内容。它将通过这个构造函数映射对象(使用Java反射概念)

    2.理解dtd。有两种类型的dtd可用:映射dtd和配置dtd

    映射dtd是

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-mapping PUBLIC
            "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
            "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    

    配置dtd

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-configuration PUBLIC
            "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
            "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    

    冬眠cfg。xml是配置文件和人员。哈佛商学院。xml正在映射文件,因此请亲自更改dtd。哈佛商学院。xml

    它会跑的。

    陆沈浪
    2023-03-14

    错误消息说(埋在一个大堆栈跟踪中):

    Document root element "hibernate-mapping", must match DOCTYPE root "hibernate-configuration"
    

    这是因为在你的erson.hbm.xml档案中

    <!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    

    你应该去的地方

    <!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    

    此DOCTYPE必须与根标记匹配。

     类似资料:
    • 我需要你帮我把冬眠映射成一对多我不知道为什么这是错的 另一类: 映射 另一个映射: Hibernate配置 这就是错误: 如果有人能帮我,我非常感激,谢谢!

    • 我正在遵循本教程,并得到以上错误。我试过这个和这个,但都没有解决问题(我的模型类所在的com.spring.hibernate.model下有user.hbm.xml,而class-path(src/main/java)下有hibernate.cfg.xml文件)。有谁能帮忙吗? user.hbm.xml hibernate.cfg.xml 我正在使用mysql,这是我更新的cfg文件

    • 问题内容: 我知道这已经被问过十二遍了。今天,我一定很沮丧,因为我尝试了一百种方法来修改xml文件,但我无法弄清楚。 运行驱动程序时出现错误堆栈: 这是我如何建立表格的MySQL代码: 这是我的映射文件: 这是我的配置文件: 任何帮助表示赞赏,谢谢! 一些其他信息:安装程序包test_hibernate和这是我的持久性文件: 这是我的司机: 问题答案: 您的映射文件看起来不错。我认为这可能是由于您

    • 当我尝试用Maven编译时,我得到了这个错误: 但我看不出错误在哪里: user.java

    • 我正在尝试从当前用户那里获取特定租金的列表。 控制器中的代码: account\u id是外键。 运行后,我发现错误: 我做错了什么?

    • 我想写一个简单的不存在的地方子句HQL查询。我不熟悉Hibernate,我有一些小问题。 我的查询如下所示: 我想选择所有包含IDUser当天购买的门票的天数。所有这些都与id为IDFest的节日相关。 我的堆栈跟踪: 映射类: Day.java Festivals.java 门票。Java语言