初始SessionFactory创建failed.org.hibernate.InvalidMappingException:无法读取XML

时间:2012-11-19 05:11:29

标签: xml hibernate

我在做this tutorial

我收到以下错误:

  

初始SessionFactory创建failed.org.hibernate.InvalidMappingException:无法读取XML

以下是完整的错误消息:

Nov 18, 2012 9:52:46 PM org.hibernate.internal.util.xml.ErrorLogger logErrors
ERROR: HHH000196: Error parsing XML (2) : Element type "generate" must be declared.
Nov 18, 2012 9:52:46 PM org.hibernate.internal.util.xml.ErrorLogger logErrors
ERROR: HHH000196: Error parsing XML (2) : The content of element type "id" must match "(meta*,column*,type?,generator?)".
Initial SessionFactory creation failed.org.hibernate.InvalidMappingException: Unable to read XML
Exception in thread "main" java.lang.ExceptionInInitializerError
    at com.mytutorial.CourseDaoImpl.<clinit>(CourseDaoImpl.java:19)
    at com.mytutorial.CourseServiceImpl.processCourse(CourseServiceImpl.java:10)
    at com.mytutorial.App.main(App.java:21)
Caused by: org.hibernate.InvalidMappingException: Unable to read XML
    at org.hibernate.internal.util.xml.MappingReader.readMappingDocument(MappingReader.java:109)
    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:2115)
    at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2087)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2067)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2020)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1935)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1914)
    at com.mytutorial.CourseDaoImpl.<clinit>(CourseDaoImpl.java:15)
    ... 2 more
Caused by: org.xml.sax.SAXParseException: Element type "generate" must be declared.
    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.handleStartElement(XMLDTDValidator.java:1929)
    at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.emptyElement(XMLDTDValidator.java:785)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:377)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2756)
    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.internal.util.xml.MappingReader.readMappingDocument(MappingReader.java:78)
    ... 13 more

hibernate.cfg.xml 如下:

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

<hibernate-configuration> 

    <session-factory>
        <!-- Database connection settings -->
        <property name="connection.driver_class">org.hsqldb.jdbcDriver</property> 
        <property name="connection.url">jdbc:hsqldb:hsql://localhost</property> 
        <property name="connection.username">sa</property>
        <property name="connection.password"></property>


        <!-- JDBC connection pool (use the built-in) --> 
        <property name="connection.pool_size">2</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.HSQLDialect</property>

        <!-- Drop and re-create the database schema on start-up, also try with “update” to keep the previous values -->
        <property name="hbm2ddl.auto">create</property> 


        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>
        <mapping resource="com/mytutorial/Course.hbm.xml"/> 
    </session-factory>
</hibernate-configuration>

Course.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.mytutorial.Course" table="Course">
        <id name="id" column="course_id">
            <generate class="native"/>
        </id>

        <property name="name" column="name"/>
        <property name="course" column="course"/>
    </class>
</hibernate-mapping>

如果有人可以帮助我,我将非常感激。

1 个答案:

答案 0 :(得分:2)

看来,Course.hbm.xml文件中存在类型错误。 ID列生成器映射的标记名称为generator。您使用generate作为<generate class="native"/>)。

请更正为<generator class="native"/>并尝试。