2016-11-01 224 views
0

在我的代碼中,我試圖從數據庫映射person_details。配置文件是如下的hibernate.cfg.xml必須聲明元素類型「hibernate-configuration」

<?xml version="1.0" encoding="utf-8"?> 
<!DOCTYPE hibernate-configuration SYSTEM 
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 

<hibernate-configuration> 
<session-factory> 
    <property name="dialect"> 
     org.hibernate.dialect.MySQLDialect 
    </property> 
    <property name="connection.driver_class"> 
     com.mysql.jdbc.Driver 
    </property> 

    <!-- Assume test is the database name --> 
    <property name="connection.url"> 
     jdbc:mysql://localhost/EMP 
    </property> 
    <property name="connection.username"> 
     root 
    </property> 
    <property name="connection.password"> 
     root 
    </property> 


    <!-- List of XML mapping files --> 
    <mapping resource="employee.hbm.xml" /> 
    <mapping class="newmavenproject.hibernateexample.CRUDS.Employee" /> 

</session-factory> 
</hibernate-configuration> 

映射XML文件如下

employee.hbm.xml

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

<hibernate-mapping> 
<class table="Employee" name="newmavenproject.hibernateexample.CRUDS.Employee"> 
    <id name="id" type="int" column="id"> 
    <generator class="native"/> 
    </id> 
    <property name="age" column="age" type="int"/> 
    <property name="firstName" column="first" type="string"/> 
    <property name="lastName" column="last" type="string"/> 

</class> 
</hibernate-mapping> 

上運行的應用我正在下面錯誤。請幫忙,因爲我無法找到任何解決這個問題的方法。

Exception in thread "main" org.hibernate.MappingException: invalid configuration 
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2241) 
at org.hibernate.cfg.Configuration.configure(Configuration.java:2158) 
at org.hibernate.cfg.Configuration.configure(Configuration.java:2137) 
at newmavenproject.hibernateexample.CRUDS.CrudsOps.main(CrudsOps.java:13) 
Caused by: org.xml.sax.SAXParseException; lineNumber: 5; columnNumber: 26; Element type "hibernate-configuration" must be declared. 
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203) 
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:134) 
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:396) 
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:327) 
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:284) 
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.handleStartElement(XMLDTDValidator.java:1906) 
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.startElement(XMLDTDValidator.java:742) 
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:374) 
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:613) 
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3132) 
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:852) 
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:602) 
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:112) 
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:505) 
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:841) 
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:770) 
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141) 
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213) 
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:643) 
at org.dom4j.io.SAXReader.read(SAXReader.java:465) 
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2238) 
... 3 more 

回答

0

你試圖定義一個Hibernate的配置,但DTD文件你在XML中引用錯誤地指向http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd。將您的DOCTYPE聲明更改爲

<!DOCTYPE hibernate-configuration PUBLIC 
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 
相關問題