Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Samson Mlati
Ranch Hand
Posts: 41
Hibernate Java Oracle
  • 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,
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate 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 Java Oracle
  • 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>

 
Rahul Babbar
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
Eclipse IDE Hibernate 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 Java Oracle
  • 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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic