org.hibernate.HibernateException: Could not parse configuration: hibernate.cfg.xml

If you are looking for a solution for org.hibernate.HibernateException: Could not parse configuration: hibernate.cfg.xml then you have come to right place. Recently I was working on a Hibernate project with latest version 4.3.5.Final and everything was going smooth. Suddenly I lost my internet connection for sometime and the project stopped working and it was throwing following exception.

org.hibernate.HibernateException: Could not parse configuration: hibernate.cfg.xml

From above exception stack trace, it seems like Hibernate is trying to load DTD file to validate the hibernate.cfg.xml file and it was failing because there was no internet connection.

I use Hibernate Tools to generate my hibernate configuration and mapping files.

My hibernate.cfg.xml file had below DTD DocType definition.

Ideally it should be not the problem because the DTD file is present in the hibernate jars and it should load it from there. But it was not happening. After spending some time looking for online help, I was able to find two ways to solve this issue.

  1. Hibernate Configuration File LocationThe first solution was to provide the DTD file location in the system using classpath. So the DocType that worked offline would be;
  2. Use SourceForge DTD URL with SYSTEMAnother solution I found working is when I change the DTD URL to SourceForge and changed the declaration from PUBLIC to SYSTEM.

    So below will also work if your system is offline.

    This seems strange because ideally it should be working for DTD URL from hibernate.org because Hibernate 4 always gives following warning.

This is good to know because sometimes our production servers are behind the firewall and may not have access to internet. These tricks can become handy in those cases. Same configurations will also work for hibernate mapping xml files.

By admin

Leave a Reply