Hibernate错误:org.xml.sax.SAXParseException:必须声明元素类型“hibernate-mapping”

时间:2012-05-07 22:30:28

标签: java hibernate

我正在使用hibernate-core-4.1.3.Final.jar。在将数据插入表中时,我收到以下错误:

org.xml.sax.SAXParseException: Element type "hibernate-mapping" must be declared.

我的映射文件如下:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated May 7, 2012 3:07:47 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.paypal.socialpay.models.User" table="user" catalog="socialdb">
    <id name="id" type="java.lang.Integer">
        <column name="id" />
        <generator class="identity" />
    </id>
    <property name="payerid" type="string">
        <column name="payerid" length="50" not-null="true" />
    </property>
    <property name="emailid" type="string">
        <column name="emailid" length="100" not-null="true" />
    </property>
    <property name="createdDt" type="timestamp">
        <column name="created_dt" length="0" not-null="true" />
    </property>
    <property name="updatedDt" type="timestamp">
        <column name="updated_dt" length="0" not-null="true" />
    </property>
    <property name="firstname" type="string">
        <column name="firstname" length="100" />
    </property>
    <property name="lastname" type="string">
        <column name="lastname" length="100" />
    </property>
    <property name="correlationid" type="string">
        <column name="correlationid" length="100" />
    </property>
</class>
</hibernate-mapping>

错误的堆栈跟踪是:

org.xml.sax.SAXParseException: Element type "hibernate-mapping" must be declared.
    at org.apache.xerces.framework.XMLParser.reportError(XMLParser.java:1213)
    at org.apache.xerces.validators.common.XMLValidator.reportRecoverableXMLError(XMLValidator.java:1807)
    at org.apache.xerces.validators.common.XMLValidator.validateElementAndAttributes(XMLValidator.java:3633)
    at org.apache.xerces.validators.common.XMLValidator.callStartElement(XMLValidator.java:1229)
    at org.apache.xerces.framework.XMLDocumentScanner$ContentDispatcher.dispatch(XMLDocumentScanner.java:938)
    at org.apache.xerces.framework.XMLDocumentScanner.parseSome(XMLDocumentScanner.java:381)
    at org.apache.xerces.framework.XMLParser.parse(XMLParser.java:1098)
    at org.dom4j.io.SAXReader.read(SAXReader.java:465)
    at org.hibernate.internal.util.xml.MappingReader.readMappingDocument(MappingReader.java:78)
    at org.hibernate.cfg.Configuration.add(Configuration.java:478)
    at org.hibernate.cfg.Configuration.add(Configuration.java:474)
    at org.hibernate.cfg.Configuration.add(Configuration.java:647)
    at org.hibernate.cfg.Configuration.addResource(Configuration.java:730)
    at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2109)
    at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2081)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2061)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2014)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1929)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1908)
    at com.paypal.utils.HibernateUtil.<clinit>(HibernateUtil.java:12)
    at com.paypal.utils.UserService.findUserByEmail(UserService.java:29)
    at com.paypal.socialpay.controllers.APIController.doGetAuthDetails(APIController.java:257)
    at com.paypal.socialpay.controllers.APIController.handleRequest(APIController.java:73)
    at com.paypal.socialpay.controllers.APIController.doPost(APIController.java:55)
    at com.paypal.socialpay.controllers.APIController.doGet(APIController.java:49)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:309)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:680)

2 个答案:

答案 0 :(得分:0)

如果在起始<hibernate-mapping>标记之前标题中存在任何非法字符,则会发生此行为。这甚至可以是不可打印的角色。

可能发生这种情况的另一个原因是错误的字符编码。我的映射文件中的第一行始终是

<?xml version="1.0" encoding="utf-8"?>

答案 1 :(得分:0)

正如Rags所提到的,你必须在你的CLASSPATH上包含适当的hibernate jar。

有关详细信息,请参阅: http://docs.jboss.org/hibernate/orm/3.6/reference/en-US/html_single/#tutorial-firstapp-mapping

Hibernate will not load the DTD file from the web, but first look it up from the classpath of 
the application. The DTD file is included in hibernate-core.jar (it is also included in the 
hibernate3.jar, if using the distribution bundle).