【问题标题】:Hibernate: Element type "hibernate-mapping" must be declaredHibernate:必须声明元素类型“hibernate-mapping”
【发布时间】:2015-03-07 07:51:04
【问题描述】:

我正在为桌面应用程序使用休眠,但我遇到了这个异常:

Failed to create sessionFactory object.org.hibernate.InvalidMappingException: Unable to read XML
Caused by: org.xml.sax.SAXParseException; lineNumber: 6; columnNumber: 20; Element type "hibernate-mapping" must be declared.

这是我的配置 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
     <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
        <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
        <property name="hibernate.connection.username">postgres</property>
        <property name="hibernate.connection.password">root</property>
        <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/test_hibernate</property>

        <mapping resource="Employee.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

这是映射 XML 文件:

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

<hibernate-mapping>
 <class name="model.Employee" table="employee">
     <meta attribute="class-description">
        This class contains the employee detail.
     </meta>
     <id name="id" type="int" column="id">
        <generator class="native"/>
     </id>
     <property name="name" column="name" type="string"/>
     <property name="salary" column="salary" type="double"/>
 </class>
</hibernate-mapping>

我做错了什么?

【问题讨论】:

  • 看来你在映射 XML 中的 DOCTYPE 定义有误,应该是&lt;!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"&gt;,注意DTD//EN 之间的3.0

标签: java swing hibernate


【解决方案1】:

hibernate-core-4.3.7.Final.jar 中的hibernate-mapping-3.0.dtd,您在Employee.hbm.xml 中的 DOCTYPE 声明应如下所示

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

代替

&lt;!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"&gt;

注意在DTD//EN 之间有一个3.0

【讨论】:

    猜你喜欢
    • 2019-11-10
    • 1970-01-01
    • 1970-01-01
    • 2019-11-09
    • 1970-01-01
    • 2015-12-17
    • 1970-01-01
    • 2019-11-20
    • 1970-01-01
    相关资源
    最近更新 更多