线程“主” org.hibernate.InvalidMappingException中的异常:无法解析资源emp.hbm.xml中的映射文档

时间:2019-06-14 23:26:25

标签: hibernate nhibernate fluent-nhibernate hibernate-mapping nhibernate-mapping

我遇到了这个问题,我查看了很多有关此错误的帖子,但没有得到正确的答案alose view this link

    > Emp Class

/ *          *要更改此许可证标题,请在“项目属性”中选择“许可证标题”。          *要更改此模板文件,请选择工具|范本          *并在编辑器中打开模板。          * /         包装车间;         导入tackshop.empClass;         / **          *          * @作者lubiesol          * /         公共类empClass {

        private int id;
        private String name;
        private String email;


        public void setId(int id){

            this.id=id;
        }

        public int getId(){

            return id;
        }

        public void setName(String name){
        this.name=name;

        }
        public String getName(){
        return name;

        }
        public void setEmail(String email){

            this.email=email;

        }
        public String getEmail(){

        return email;

        }

    }


    > Main class

    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package tackshop;

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

    /**
     *
     * @author lubiesol
     */
    public class Tackshop {

        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {

            Configuration con = new Configuration();
            con.configure("hibernate.cfg.xml");
            SessionFactory sf = con.buildSessionFactory();
            Session session = sf.openSession();
            Transaction tx = session.beginTransaction();

            empClass u = new empClass();
            u.setId(111);
            u.setName("xyz");
            u.setEmail("xyz@gmail.com");
            session.save(u);
            tx.commit();
        }
    }

> hibernate.cfg.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-configuration>
      <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/demo</property>
        <property name="hibernate.connection.username">ultronhouse</property>
        <property name="hibernate.connection.password">qwertyuiop</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        <property name="hbm2ddl.auto">update </property>
        <mapping resource="emp.hbm.xml"/>
        <mapping resource="emp.hbm.xml"/>
      </session-factory>
    </hibernate-configuration>
> emp.hbm.xml

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

<hibernate-mapping>
<class name="tackshop.empClass" table="user">

<id name="id" column="id">
</id>

<property name="name" ></property>
<property name="email" ></property>


</class>

</hibernate-mapping>
  

错误详细信息

Exception in thread "main" org.hibernate.InvalidMappingException: Could not parse mapping document from resource emp.hbm.xml
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXml(Configuration.java:3764)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXmlQueue(Configuration.java:3753)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3741)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1410)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1928)
    at tackshop.Tackshop.main(Tackshop.java:27)
Caused by: org.hibernate.DuplicateMappingException: Duplicate class/entity mapping tackshop.empClass
    at org.hibernate.cfg.Configuration$MappingsImpl.addClass(Configuration.java:2837)
    at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:178)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXml(Configuration.java:3761)
    ... 6 more

ErrorImage.png

1 个答案:

答案 0 :(得分:0)

似乎其中一个问题与翻倍 <mapping resource="emp.hbm.xml"/>映射有关:

<hibernate-configuration>
  <session-factory>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/demo</property>
    ...
    <mapping resource="emp.hbm.xml"/>
    <mapping resource="emp.hbm.xml"/> <!-- remove this -->

因此

  

org.hibernate.DuplicateMappingException:重复的类/实体映射tackshop.empClass