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
的解决方案org.hibernate.HibernateException: Could not parse configuration: hibernate.cfg.xml
那么您来对地方了。 最近我正在开发一个Hibernate项目,最新版本为4.3.5.Final,一切进展顺利。 突然我失去了一段时间的互联网连接,该项目停止工作,并引发异常。
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:无法解析配置: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.
从上面的异常堆栈跟踪看来,Hibernate似乎正在尝试加载DTD文件以验证hibernate.cfg.xml文件,但是由于没有Internet连接而失败了。
I use Hibernate Tools to generate my hibernate configuration and mapping files.
我使用Hibernate Tools生成我的hibernate配置和映射文件。
My hibernate.cfg.xml
file had below DTD DocType definition.
我的hibernate.cfg.xml
文件具有以下DTD DocType定义。
<!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罐中存在DTD文件,并且应该从那里加载它。 但这没有发生。 花了一些时间寻求在线帮助后,我能够找到两种方法来解决此问题。
- 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;
Hibernate配置文件位置<!DOCTYPE hibernate-configuration SYSTEM "classpath://org/hibernate/hibernate-configuration-3.0.dtd">
第一个解决方案是使用类路径在系统中提供DTD文件位置。 因此,可以脱机工作的DocType应该是:
- 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.
将SourceForge DTD URL与SYSTEM一起使用我发现工作的另一个解决方案是将DTD URL更改为SourceForge,并将声明从PUBLIC更改为SYSTEM。
因此,如果您的系统处于离线状态,则下面的内容同样适用。
<!DOCTYPE hibernate-configuration SYSTEM "https://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
这似乎很奇怪,因为理想情况下它应该适用于hibernate.org的DTD URL,因为Hibernate 4始终会发出以下警告。
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.
很高兴知道这一点,因为有时我们的生产服务器位于防火墙后面,可能无法访问Internet。 在这些情况下,这些技巧可以派上用场。 Hibernate映射xml文件也可以使用相同的配置。