1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
Initial SessionFactory creation failed.org.hibernate.HibernateException: Could not parse configuration: hibernate.cfg.xml org.hibernate.HibernateException: Could not parse configuration: hibernate.cfg.xml at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2163) at org.hibernate.cfg.Configuration.configure(Configuration.java:2075) at com.journaldev.hibernate.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:16) at com.journaldev.hibernate.util.HibernateUtil.getSessionFactory(HibernateUtil.java:34) at com.journaldev.hibernate.main.HQLExamples.main(HQLExamples.java:20) Caused by: org.dom4j.DocumentException: hibernate.org Nested exception: hibernate.org at org.dom4j.io.SAXReader.read(SAXReader.java:484) at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2155) ... 4 more Exception in thread "main" java.lang.ExceptionInInitializerError at com.journaldev.hibernate.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:29) at com.journaldev.hibernate.util.HibernateUtil.getSessionFactory(HibernateUtil.java:34) at com.journaldev.hibernate.main.HQLExamples.main(HQLExamples.java:20) Caused by: org.hibernate.HibernateException: Could not parse configuration: hibernate.cfg.xml at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2163) at org.hibernate.cfg.Configuration.configure(Configuration.java:2075) at com.journaldev.hibernate.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:16) ... 2 more Caused by: org.dom4j.DocumentException: hibernate.org Nested exception: hibernate.org at org.dom4j.io.SAXReader.read(SAXReader.java:484) at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2155) ... 4 more |
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.
1 2 3 4 5 |
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "https://hibernate.org/dtd/hibernate-configuration-3.0.dtd"> |
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.
- 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;
1234<!DOCTYPE hibernate-configuration SYSTEM"classpath://org/hibernate/hibernate-configuration-3.0.dtd"> - 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.
1234<!DOCTYPE hibernate-configuration SYSTEM"https://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
This seems strange because ideally it should be working for DTD URL from hibernate.org because Hibernate 4 always gives following warning.
1234WARN: HHH000223: Recognized obsolete hibernate namespace https://hibernate.sourceforge.net/.Use namespace https://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
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.