【发布时间】: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 定义有误,应该是
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">,注意DTD和//EN之间的3.0