文档根元素“hibernate-configuration”,必须匹配DOCTYPE根“hibernate-mapping”

时间:2013-08-26 10:38:06

标签: hibernate

在我的代码中我试图从数据库中映射person_details。配置文件如下hibernate.cfg.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-configuration>
    <session-factory>
        <property name="connection.url">
            jdbc:mysql://localhost/test
        </property>
        <property name="connection.driver_class">
            com.mysql.jdbc.Driver
        </property>
        <property name="hibernate.connection.username">
            root
        </property>
        <property name="connection.password">
            gopal
        </property>
        <property name="connection.pool_size">
            1
        </property>
        <property name="hibernate.dialect">
            org.hibernate.dialect.HSQLDialect
        </property>
        <property name="show_sql">
            true
        </property>
        <property name="hbm2ddl.auto">
            create
        </property>
        <mapping resource="com/sixdee/persondetail/pesondetail.hbm.xml" />
    </session-factory>
</hibernate-configuration>

映射xml文件如下

persondetail.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="com.sixdee.Persondetail" table="person_details">
        <id name="id" type="integer" column="EMP_ID">
            <generator class="assigned" />
        </id>
        <property name="gender" type="String" column="GENDER" />
        <property name="age" type="integer" colummn="AGE" />
        <property name="dob" type="String" column="DOB" />
    </class>
</Hibernate-mapping>

主要课程如下 persondetailmain.java

    package com.sixdee.persondetail;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;



public class Persondetailmain {
    public static void main(String[] args) {
        Session session = null;
        try{
            SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
            session=sessionFactory.openSession();
            Transaction transaction=null;
            transaction=session.beginTransaction();
            Persondetail person=new Persondetail(3,"male",23,"12-06-1990");
            session.save(person);
            transaction.commit();
        }
        catch (NullPointerException e) {
            e.printStackTrace();
        }
        catch (HibernateException e) {
            e.printStackTrace();

        }

        finally{
            if(session!=null){
                session.close();
                session=null;
            }

        }


    }

}

在运行main方法时遇到以下错误....

    org.hibernate.MappingException: invalid configuration
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1521)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1462)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1448)
    at com.sixdee.persondetail.Persondetailmain.main(Persondetailmain.java:15)
Caused by: org.xml.sax.SAXParseException: Document root element "hibernate-configuration", must match DOCTYPE root "hibernate-mapping".
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:131)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:384)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:318)
    at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.rootElementSpecified(XMLDTDValidator.java:1621)
    at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.handleStartElement(XMLDTDValidator.java:1900)
    at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.startElement(XMLDTDValidator.java:764)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:400)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:626)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3104)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:921)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:647)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:511)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:808)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
    at org.dom4j.io.SAXReader.read(SAXReader.java:465)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1518)
    ... 3 more

请帮帮我

2 个答案:

答案 0 :(得分:4)

<hibernate-configuration>的DOCTYPE应为:

<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

答案 1 :(得分:0)

虽然上述方法有效,但您可以在DOCTYPE(hibernate-mapping)中使用<hibernate-configuration> persondetail.hbm.xml标记。