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
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"
"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 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">
- 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 "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.
WARN: 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.
Hi Pankaj
Thanks for providing this troubleshooting.
I had a similar problem, but in my case the exception thrown was something like this:
Java.net.ConnectException: Tried all: ‘1’ addresses, but could not connect over HTTP to server: ‘www.hibernate.org’,
….
In fact, I noticed that I had different DOCTYPE declarations in two hbm files:
1) Working one:
2) Not working one:
After changing the 2) with 1), everything started working fine.
Hope this can help someone who is having same issue.
Best regards.
Marco.
Maven + Hibernate + MySQL
Sep 24, 2019 11:40:41 AM org.hibernate.cfg.Environment
INFO: Hibernate 3.2.3
Sep 24, 2019 11:40:41 AM org.hibernate.cfg.Environment
INFO: hibernate.properties not found
Sep 24, 2019 11:40:41 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: Bytecode provider name : cglib
Sep 24, 2019 11:40:41 AM org.hibernate.cfg.Environment
INFO: using JDK 1.4 java.sql.Timestamp handling
Sep 24, 2019 11:40:41 AM org.hibernate.cfg.Configuration configure
INFO: configuring from resource: /hibernate.cfg.xml
Sep 24, 2019 11:40:41 AM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: Configuration resource: /hibernate.cfg.xml
Sep 24, 2019 11:40:41 AM org.hibernate.cfg.Configuration addResource
INFO: Reading mappings from resource : hbm/question.hbm.xml
Initial SessionFactory creation failed.org.hibernate.InvalidMappingException: Could not parse mapping document from resource hbm/question.hbm.xml
Exception in thread “main” java.lang.ExceptionInInitializerError
at com.java.common.HibernateUtil.buildSessionFactory(HibernateUtil.java:19)
at com.java.common.HibernateUtil.(HibernateUtil.java:9)
at com.java.common.StoreData.main(StoreData.java:11)
Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from resource hbm/question.hbm.xml
at org.hibernate.cfg.Configuration.addResource(Configuration.java:569)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1587)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1555)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1534)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1508)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1428)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1414)
at com.java.common.HibernateUtil.buildSessionFactory(HibernateUtil.java:14)
… 2 more
Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from input stream
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:508)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:566)
… 9 more
Caused by: org.dom4j.DocumentException: https://hibernate.org/dtd/hibernate-mapping-5.3.dtd Nested exception: https://hibernate.org/dtd/hibernate-mapping-5.3.dtd
at org.dom4j.io.SAXReader.read(SAXReader.java:484)
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:499)
… 10 more
Thanks Pankaj. Its a great help even I faced same issue and resolved with your solution.
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
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See https://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)
i am created simple java project
why cfg .xmlnot found
changing URL “https://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd” has worked for me. Thanks
Am getting error could not parse configuration :hibernate. Christmas. XML file. Please any one give solution for this problem ..
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
Thanks!!! I have been struggling with this error for over 2 hours now (which suddenly popped up from nowhere).
I am facing following problem
org.hibernate.MappingException: An AnnotationConfiguration instance is required to use
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
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
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.
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.
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)…
Hi dude,
Could you post hibernate interview questions? that would help my interview preparation also I could learn more about hibernate.