0

在我的代码中,我试图从数据库中映射 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文件如下

人物细节.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;
            }

        }


    }

}

在运行主要方法时我得到以下错误....

    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

请帮帮我

4

2 回答 2

5

DOCTYPE<hibernate-configuration>应该是:

<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
于 2013-08-26T11:46:57.210 回答
0

虽然上述方法有效,但您可以使用DOCTYPE(hibernate-mapping)with <hibernate-configuration>tag in persondetail.hbm.xml

于 2014-07-29T15:03:20.923 回答