Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 12 posts ] 
Author Message
 Post subject: Could not parse configuration: /hibernate.cfg.xml
PostPosted: Fri Feb 20, 2009 6:08 am 
Newbie

Joined: Thu Feb 19, 2009 12:24 pm
Posts: 14
Hello,

I am a newbie and hope someone can help me with this. I am trying to learn Hibernate by using the tutorial, but I got stuck here. I am getting parsing error when Ant tries to compile my project. Please see all data below. Thanks in advance!

Hibernate version:
3.3.1.GA

Mapping documents:
Code:
<?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="events.Event" table="EVENTS">
        <id name="id" column="EVENT_ID">
            <generator class="native"/>
        </id>
        <property name="date" type="timestamp" column="EVENT_DATE"/>
        <property name="title"/>
    </class>

</hibernate-mapping>




Code between sessionFactory.openSession() and session.close():

Code:
     Session session = HibernateUtil.getSessionFactory().getCurrentSession();

        session.beginTransaction();

        Event theEvent = new Event();
        theEvent.setTitle(title);
        theEvent.setDate(theDate);

        session.save(theEvent);

        session.getTransaction().commit();



Build.xml

Code:
<project name="hibernate-tutorial" default="run">

    <property name="sourcedir" value="${basedir}/src"/>
    <property name="targetdir" value="${basedir}/bin"/>
    <property name="librarydir" value="${basedir}/lib"/>

    <path id="libraries">
        <fileset dir="${librarydir}">
            <include name="*.jar"/>
            <include name="bytecode/*.jar"/>
            <include name="optional/*.jar"/>
            <include name="required/*.jar"/>
        </fileset>
    </path>

    <target name="clean">
        <delete dir="${targetdir}"/>
        <mkdir dir="${targetdir}"/>
    </target>

    <target name="compile" depends="clean, copy-resources">
      <javac srcdir="${sourcedir}"
             destdir="${targetdir}"
             classpathref="libraries"/>
    </target>

    <target name="copy-resources">
        <copy todir="${targetdir}">
            <fileset dir="${sourcedir}">
                <exclude name="**/*.java"/>
            </fileset>
        </copy>
    </target>

   <target name="run" depends="compile">
       <java fork="true" classname="events.EventManager" classpathref="libraries">
           <classpath path="${targetdir}"/>
           <arg value="${action}"/>
       </java>
   </target>

</project>


Full stack trace of any exception that occurs:
[java] 2009-feb-20 10:50:55 org.hibernate.cfg.Environment <clinit>
[java] INFO: Hibernate 3.3.1.GA
[java] 2009-feb-20 10:50:56 org.hibernate.cfg.Environment <clinit>
[java] INFO: loaded properties from resource hibernate.properties: {hiberna
te.connection.driver_class=org.hsqldb.jdbcDriver, hibernate.cache.provider_class
=org.hibernate.cache.HashtableCacheProvider, hibernate.max_fetch_depth=1, hibern
ate.dialect=org.hibernate.dialect.HSQLDialect, hibernate.jdbc.use_streams_for_bi
nary=true, hibernate.format_sql=true, hibernate.query.substitutions=yes 'Y', no
'N', hibernate.proxool.pool_alias=pool1, hibernate.connection.username=sa, hiber
nate.cache.region_prefix=hibernate.test, hibernate.connection.url=jdbc:hsqldb:./
build/db/hsqldb/hibernate, hibernate.bytecode.use_reflection_optimizer=false, hi
bernate.connection.password=****, hibernate.jdbc.batch_versioned_data=true, hibe
rnate.connection.pool_size=1}
[java] 2009-feb-20 10:50:56 org.hibernate.cfg.Environment <clinit>
[java] INFO: using java.io streams to persist binary types
[java] 2009-feb-20 10:50:56 org.hibernate.cfg.Environment buildBytecodeProv
ider
[java] INFO: Bytecode provider name : javassist
[java] 2009-feb-20 10:50:56 org.hibernate.cfg.Environment <clinit>
[java] INFO: using JDK 1.4 java.sql.Timestamp handling
[java] 2009-feb-20 10:50:56 org.hibernate.cfg.Configuration configure
[java] INFO: configuring from resource: /hibernate.cfg.xml
[java] 2009-feb-20 10:50:56 org.hibernate.cfg.Configuration getConfiguratio
nInputStream
[java] INFO: Configuration resource: /hibernate.cfg.xml
[java] 2009-feb-20 10:50:56 org.hibernate.util.XMLHelper$ErrorLogger error
[java] ALLVARLIG: Error parsing XML: /hibernate.cfg.xml(2) Content is not a
llowed in prolog.
[java] Initial SessionFactory creation failed.org.hibernate.HibernateExcept
ion: Could not parse configuration: /hibernate.cfg.xml
[java] Exception in thread "Main Thread" java.lang.ExceptionInInitializerEr
ror
[java] at util.HibernateUtil.<clinit>(Unknown Source)
[java] at events.EventManager.createAndStoreEvent(Unknown Source)
[java] at events.EventManager.main(Unknown Source)
[java] Caused by: org.hibernate.HibernateException: Could not parse configu
ration: /hibernate.cfg.xml
[java] at org.hibernate.cfg.Configuration.doConfigure(Configuration.jav
a:1528)
[java] at org.hibernate.cfg.Configuration.configure(Configuration.java:
1462)
[java] at org.hibernate.cfg.Configuration.configure(Configuration.java:
1448)
[java] ... 3 more
[java] Caused by: org.dom4j.DocumentException: Error on line 2 of document
: Content is not allowed in prolog. Nested exception: Content is not allowed in
prolog.
[java] at org.dom4j.io.SAXReader.read(SAXReader.java:482)
[java] at org.hibernate.cfg.Configuration.doConfigure(Configuration.jav
a:1518)
[java] ... 5 more
[java] Java Result: 1



Name and version of the database you are using:
HSQLDB 1.8.0.10

Best Regards,
Niklas

_________________
Beginners must also learn!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 20, 2009 6:46 am 
Beginner
Beginner

Joined: Thu Feb 19, 2009 5:48 am
Posts: 37
Location: Glasgow, Scotland
You need to post your hibernate.cfg.xml file, it seems like there's an error with it.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 20, 2009 8:06 am 
Newbie

Joined: Thu Feb 19, 2009 12:24 pm
Posts: 14
Ok, here it is:

hibernate.cfg.xml:

Code:
<?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">1</property>

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

        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">create</property>

        <mapping resource="events/Event.hbm.xml"/>

    </session-factory>

</hibernate-configuration>

_________________
Beginners must also learn!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 20, 2009 8:26 am 
Beginner
Beginner

Joined: Thu Feb 19, 2009 5:48 am
Posts: 37
Location: Glasgow, Scotland
Nope, that all seems to be in order. Maybe you have a missing or out of date dependancy JAR file?!?

_________________
##############################
If I helped, rate my comment, I have plenty of stupid questions to ask that I need credit for ;)
##############################


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 20, 2009 9:44 am 
Senior
Senior

Joined: Tue Aug 01, 2006 9:24 pm
Posts: 120
That looks correct to me also. Are you sure there isn't some strange characters on the top of that file maybe just be sure to remove any line breaks and spaces.

Also possibly if you are opening this up on your local editor ie notepad is it saving correctly maybe it's saving in dos format when it should be unix(or vice versa). I know I once had trouble with osx's textpad and xml files awhile back.

Other than that rosswilliamdrew is probably right, just check all your libraries make sure they are compatible.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 20, 2009 10:12 am 
Newbie

Joined: Thu Feb 19, 2009 12:24 pm
Posts: 14
I removed the spaces and line breakes, no change in behaviour.

Maybe a dumb question but how do I verify the compatibility of the libraries?

_________________
Beginners must also learn!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 20, 2009 10:16 am 
Beginner
Beginner

Joined: Thu Feb 19, 2009 5:48 am
Posts: 37
Location: Glasgow, Scotland
Nicsoft wrote:
I removed the spaces and line breakes, no change in behaviour.

Maybe a dumb question but how do I verify the compatibility of the libraries?


Trial and error I'm afraid :(

_________________
##############################
If I helped, rate my comment, I have plenty of stupid questions to ask that I need credit for ;)
##############################


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 23, 2009 7:13 am 
Newbie

Joined: Thu Feb 19, 2009 12:24 pm
Posts: 14
I solved the problem by removing the encoding from the XML-declaration.

I.e.
Code:
<?xml version='1.0' encoding='utf-8'?>
was changed to
Code:
<?xml version='1.0'?>


I have no idea why this solved the problem, but it did.

Thanks for your time!

_________________
Beginners must also learn!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 23, 2009 8:58 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
It probably solved your problem because your file is not stored in utf-8 but e.g. Unicode...

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 23, 2009 9:35 am 
Newbie

Joined: Thu Feb 19, 2009 12:24 pm
Posts: 14
Correct! It's possible to chose in Notepd and Unicode was choosen...

_________________
Beginners must also learn!


Top
 Profile  
 
 Post subject: Re: Could not parse configuration: /hibernate.cfg.xml
PostPosted: Mon May 02, 2011 12:05 pm 
Regular
Regular

Joined: Fri Feb 09, 2007 3:47 pm
Posts: 56
gread i add <?xml version='1.0' encoding='utf-8'?> to fix this problem.











gwvd


Top
 Profile  
 
 Post subject: Re: Could not parse configuration: /hibernate.cfg.xml
PostPosted: Fri Feb 01, 2013 11:28 am 
Newbie

Joined: Fri Feb 01, 2013 11:22 am
Posts: 1
i have removed encoding='utf-8'
but now that showing <a href="http://dlooker.com">error?</a>
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.hibernate.MappingException: invalid configuration
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1484)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1425)
at dataDAO.EmployeeDAO.main(EmployeeDAO.java:14)
Caused by: org.xml.sax.SAXParseException: The content of element type "session-factory" must match "(property*,mapping*,(class-cache|collection-cache)*,event*,listener*)".
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.dtd.XMLDTDValidator.handleEndElement(Unknown Source)
at org.apache.xerces.impl.dtd.XMLDTDValidator.endElement(Unknown Source)
at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanEndElement(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.dom4j.io.SAXReader.read(SAXReader.java:465)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1481)
... 2 <a href="http://searchingwhat.com">more</a>


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 12 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.