3

I am doing this tutorial

I am getting this following error:

Initial SessionFactory creation failed.org.hibernate.InvalidMappingException: Unable to read XML

Here is full error message:

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 is the following:

<?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>

I would be really appreciated if someone can help me.

1 Answer 1

2

It seems, you have a type error in Course.hbm.xml file. The tag name for ID column generator mapping is generator. You are using generate as <generate class="native"/>).

Please correct as <generator class="native"/> and try.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your answer! it corrects the type error but now I have a different error. Would you mind taking a look at this error message? ERROR: java.net.ConnectException: Connection refused Exception in thread "main" org.hibernate.exception.JDBCConnectionException: Could not open connection and Caused by: java.sql.SQLTransientConnectionException: java.net.ConnectException: Connection refused and Caused by: org.hsqldb.HsqlException: java.net.ConnectException: Connection refused and Caused by: java.net.ConnectException: Connection refused Thank you!
@user1834781: I think its a different question related to HSQL setup and configuration. Can you post a new question with full stack trace as there should be some more information in the stack trace?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.