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

Filed Under: Hibernate

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.


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

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.


<!DOCTYPE hibernate-configuration PUBLIC
		"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
		"http://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.

  1. Hibernate Configuration File Location

    The first solution was to provide the DTD file location in the system using classpath. So the DocType that worked offline would be;

    
    <!DOCTYPE hibernate-configuration SYSTEM 
        "classpath://org/hibernate/hibernate-configuration-3.0.dtd">
    
  2. Use SourceForge DTD URL with SYSTEM

    Another 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.

    
    <!DOCTYPE hibernate-configuration SYSTEM 
        "http://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.

    
    WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. 
    Use namespace http://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.

Comments

  1. Dnyanesh says:

    Thanks Pankaj. Its a great help even I faced same issue and resolved with your solution.

  2. mahesh Rathikindi says:

    Oct 03, 2018 2:41:01 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager
    INFO: HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
    Oct 03, 2018 2:41:01 PM org.hibernate.Version logVersion
    INFO: HHH000412: Hibernate Core {4.3.11.Final}
    Oct 03, 2018 2:41:01 PM org.hibernate.cfg.Environment
    INFO: HHH000206: hibernate.properties not found
    Oct 03, 2018 2:41:01 PM org.hibernate.cfg.Environment buildBytecodeProvider
    INFO: HHH000021: Bytecode provider name : javassist
    Oct 03, 2018 2:41:01 PM org.hibernate.cfg.Configuration configure
    INFO: HHH000043: Configuring from resource: com/sathya/config/hibernate.cfg.xml
    Oct 03, 2018 2:41:01 PM org.hibernate.cfg.Configuration getConfigurationInputStream
    INFO: HHH000040: Configuration resource: com/sathya/config/hibernate.cfg.xml
    Exception in thread “main” org.hibernate.HibernateException: Could not parse configuration: com/sathya/config/hibernate.cfg.xml
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2165)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:2077)
    at com.sathya.test.CreateAccount.main(CreateAccount.java:17)
    Caused by: org.dom4j.DocumentException: Error on line 6 of document : The string “–” is not permitted within comments. Nested exception: The string “–” is not permitted within comments.
    at org.dom4j.io.SAXReader.read(SAXReader.java:482)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2157)
    … 2 more

  3. devendra says:

    log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
    log4j:WARN Please initialize the log4j system properly.
    log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
    Exception in thread “main” org.hibernate.MappingNotFoundException: resource: hibernateproj.dto.userdetail not found
    at org.hibernate.cfg.Configuration.addResource(Configuration.java:569)
    at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1593)
    at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1561)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1540)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1514)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1434)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1420)
    at hibernateproj.model.hibernateTest.main(hibernateTest.java:14)

    1. devendra says:

      i am created simple java project
      why cfg .xmlnot found

  4. sachindra says:

    changing URL “http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd” has worked for me. Thanks

  5. GunaSanthu says:

    Am getting error could not parse configuration :hibernate. Christmas. XML file. Please any one give solution for this problem ..

  6. Hermenegildo says:

    for me that error was caused because i have put the hibernate.config.xml in /WEB-INF. So i removed and /src/main/resources. Ind in web.xml file i wrote:

    contextConfigLocation

    classpath:hibernate.cfg.xml
    classpath:spring-config.xml

  7. barani says:

    Thanks!!! I have been struggling with this error for over 2 hours now (which suddenly popped up from nowhere).

  8. Sudhir Kumar Ojha says:

    I am facing following problem

    org.hibernate.MappingException: An AnnotationConfiguration instance is required to use

  9. Parjanya Roy says:

    Thanks ,

    Even though I was connected to the internet somehow the proxy was blocking the url access. So this worked like a charm .. Thnx a million

  10. Swapnil Solunke says:

    Hello Pankaj,

    Thanks for this solution. I was really getting problems when i used to offline but now my problem is solved.

    Thanks

    Regards
    Swapnil Solunke

  11. Niraj Gurav says:

    This one is a temporary solution. Your Hibernate jars contains dtd for validating your configuration xml. Extract ‘hibernate-configuration-3.0.dtd’ and put it in some directory in your project structure (in this case, I have put it in Project root directory). Add your dtd location to DOCTYPE declaration.

    <!DOCTYPE hibernate-configuration SYSTEM
    "hibernate-configuration-3.0.dtd">

    It worked for me. It works when system is offline. Fetches DTD from your local system.

    Thanks Pankaj. It is very similar to your classpath solution. But with classpath it says :

    Caused by: org.dom4j.DocumentException: unknown protocol: classpath Nested exception: unknown protocol: classpath

    I might be missing something.

  12. Anoop Abbot says:

    I’m hitting the below error:

    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.HibernateException: Could not parse configuration: hibernate.cfg.xml
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1491)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1425)
    at com.javatpoint.mypackage.StoreData.main(StoreData.java:13)
    Caused by: org.dom4j.DocumentException: Connection timed out: connect Nested exception: Connection timed out: connect
    at org.dom4j.io.SAXReader.read(SAXReader.java:484)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1481)
    … 2 more

    and this is the hibernate.cfg.xml file which i coded…

    com.mysql.jdbc.Driver
    jdbc:mysql://localhost/abcde
    root
    nancy123456
    org.hibernate.dialect.MySQLDialect
    update

    Please anyone have a look and provide it’s solution.

    Thanks for you help.

  13. Sairam Rajulapati says:

    Hi Pankaj,
    i try to configure hibernates on offline, according to your suggestion i changed my hibernate configuration file

    to

    still now i got some error like following one

    Exception in thread “main” org.hibernate.HibernateException: Could not parse configuration: hibernate.cfg.xml
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1491)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1425)
    at com.hib.bo.StoreData.main(StoreData.java:14)
    Caused by: org.dom4j.DocumentException: hibernate.sourceforge.net Nested exception: hibernate.sourceforge.net
    at org.dom4j.io.SAXReader.read(SAXReader.java:484)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1481)
    … 2 more

    can u suggestion me for how to resolve the problem( configure the hibernate in offline)…

  14. Rakesh says:

    Hi dude,

    Could you post hibernate interview questions? that would help my interview preparation also I could learn more about hibernate.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages