Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Hibernate: Could not parse configuration: /hibernate.cfg.xml

 
Ranch Hand
Posts: 41
Hibernate Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,


I have a consistent problem with hibernate when trying to map my classes to a mysql database. I get the error Could not parse configuration: /hibernate.cfg.xml. Here's the annoying part, sometimes the hibernate works perfectly without me changing my ANT build files or my code other times the same code gives me this error. I would like to know if this is a problem with hibernate. I have hibernate.cfg.xml in my classpath when building my code. Any ideas why this inconsistency occurs?

Best,
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your first error means one of two things, either hibernate.cfg.xml is invalid XML or its not on the classpath. The rest of the stack trace should tell you which.
 
Samson Mlati
Ranch Hand
Posts: 41
Hibernate Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Sturrock wrote:Your first error means one of two things, either hibernate.cfg.xml is invalid XML or its not on the classpath. The rest of the stack trace should tell you which.



Well like i said sometimes the code works and sometimes it throws this error. I'm sure my hibernate.cfg is fine because i generate it using xdoclet unless xdoclet generates an invalid file. Here is a listing of the file though. And i can tell its in the classpath because the error says (cannot parse hibernate.cfg.xml) meaning it finds it, just can't parse it.

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

<!-- Generated file - Do not edit! -->

<hibernate-configuration>

<!-- a SessionFactory instance listed as /jndi/name -->
<session-factory>

<!-- properties -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">false</property>
<property name="use_outer_join">false</property>
<property name="hibernate.connection.username">***</property>
<property name="hibernate.connection.password">***</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/test</property>
<property name="hibernate.connection.pool_size">20</property>

<!-- mapping files -->
<mapping resource="com/model/Role.hbm.xml"/>
<mapping resource="com/model/Car.hbm.xml"/>
<mapping resource="com/model/Person.hbm.xml"/>
</session-factory>

</hibernate-configuration>

 
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,

I am not sure if this information will help(since the issue itself cannot be reproduced consistently)

There should not be any space or line gap between the start of the file and "<?xml version="1.0" encoding="UTF-8"?> "

If you give some empty space before the above, you will get the error "cannot parse configuration" (which mainly comes if your xml is invalid)...
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Well like i said sometimes the code works and sometimes it throws this error


Software doesn't behave in this indesciminate a way. When the error occurs you must be doing something differently. Have a look at what you are doing. Double check what is deployed and how. Are there old versions of code/files kicking around?


And i can tell its in the classpath because the error says (cannot parse hibernate.cfg.xml) meaning it finds it, just can't parse it.


No, Hibernate returns the same error if the config file is not found (because it can't be parsed). If hibernate.cfg.xml was on the classpath and the XML were invalid you would see the parser tell you where and how it is invalid in your stack trace.

(Also, you are using Hibernate 2 - I'd upgrade)
 
Samson Mlati
Ranch Hand
Posts: 41
Hibernate Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Sturrock wrote:


Well like i said sometimes the code works and sometimes it throws this error


Software doesn't behave in this indesciminate a way. When the error occurs you must be doing something differently. Have a look at what you are doing. Double check what is deployed and how. Are there old versions of code/files kicking around?


And i can tell its in the classpath because the error says (cannot parse hibernate.cfg.xml) meaning it finds it, just can't parse it.


No, Hibernate returns the same error if the config file is not found (because it can't be parsed). If hibernate.cfg.xml was on the classpath and the XML were invalid you would see the parser tell you where and how it is invalid in your stack trace.

(Also, you are using Hibernate 2 - I'd upgrade)



That makes sense. Thanks for your help. This last hint was especially very helpful. After I changed my DTD's to hibernate 3.0 it works perfectly.
 
Run away! Run away! Here, take this tiny ad with you:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic