Исключение в потоке "main" org.hibernate.InvalidMappingException: не удалось проанализировать документ сопоставления из ресурса emp.hbm.xml. - PullRequest
0 голосов
/ 15 июня 2019

У меня есть проблемаЗаголовки лицензий в свойствах проекта.* Чтобы изменить этот файл шаблона, выберите Инструменты |Шаблоны * и откройте шаблон в редакторе.* / Пакет Tackshop;import tackshop.empClass;/ ** * * @author 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 голосов
/ 17 июня 2019

Кажется, что одна из проблем связана с отображением удвоенного <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

...