【问题标题】:Invalid Mapping Exception in hibernate.hbm.xmlhibernate.hbm.xml 中的无效映射异常
【发布时间】:2017-02-18 16:21:33
【问题描述】:

我正在发布我的 hbm 文件和 cfg 文件以及我的错误日志。我发现我基于类似问题的问题也尝试了他们的解决方案,但我的错误仍然没有消失。

日志

Initial SessionFactory creation   failed.org.hibernate.InvalidMappingException: Could not parse mapping document from resource net/viralpatel/hibernate/Employee.hbm.xml
Exception in thread "main" java.lang.ExceptionInInitializerError
at   net.viralpatel.hibernate.HibernateUtil.buildSessionFactory(HibernateUtil.java:18)
  at net.viralpatel.hibernate.HibernateUtil.<clinit>(HibernateUtil.java:8)
at net.viralpatel.hibernate.Main.main(Main.java:13)
 Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from resource net/viralpatel/hibernate/Employee.hbm.xml
at org.hibernate.cfg.Configuration.addResource(Configuration.java:569)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1584)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1552)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1531)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1505)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1425)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1411)
at net.viralpatel.hibernate.HibernateUtil.buildSessionFactory(HibernateUtil.java:14)
... 2 more
Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from input stream
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:508)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:566)
... 9 more
Caused by: org.dom4j.DocumentException: Error on line 9 of document  : The element type "generator" must be terminated by the matching end-tag "</generator>". Nested exception: The element type "generator" must be terminated by the matching end-tag "</generator>".
at org.dom4j.io.SAXReader.read(SAXReader.java:482)
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:499)
... 10 more

Employee.hbm.xml

 <?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">
 <hibernate-mapping>
<class name="Employee" table="EMPLOYEE" >
    <id name="employeeId" column="EMPLOYEE_ID">
       <generator class="native"> 
    </id>
    <one-to-one name="employeeDetail" class="net.viralpatel.hibernate.EmployeeDetail"
        cascade="save-update"></one-to-one>

    <property name="firstname" column="firstname" />
    <property name="lastname" column="lastname" />
    <property name="birthDate" type="date" column="birth_date" />
    <property name="cellphone" column="cell_phone" />
    </class>

hibernate.cfg.xml

   <!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="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property   name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
    <property name="connection.username">root</property>
    <property name="connection.password">tcs@1234</property>

    <property name="connection.pool_size">1</property>
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="current_session_context_class">thread</property>
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
    <property name="show_sql">true</property>
    <property name="hbm2ddl.auto">update</property>

    <mapping resource="net/viralpatel/hibernate/EmployeeDetail.hbm.xml"/>
    <mapping resource="net/viralpatel/hibernate/Employee.hbm.xml"/>

</session-factory>

【问题讨论】:

    标签: java xml hibernate hbm2ddl


    【解决方案1】:

    通过查看堆栈跟踪,您没有正确结束生成器标记。

    请更改您的employee.hbm.xml 文件以正确结束&lt;generator&gt; 元素。

    例如:应该是&lt;generator class="native"/&gt;

    【讨论】:

      【解决方案2】:

      在您的Employee.hbm.xml 中,您没有关闭生成器标签。 替换

      <generator class="native">
      

      通过这个

      <generator class="native"/> 
      

      【讨论】:

        猜你喜欢
        • 2012-03-11
        • 1970-01-01
        • 1970-01-01
        • 2015-08-12
        • 2022-01-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多