Hibernate.cfg.xml parsing error

2019-08-30 08:16发布

问题:

<

!DOCTYPE hibernate-configuration PUBLIC  
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  

I have this in my hibernate.cfg.xml file. I have the jars downloaded (from http://www.javatpoint.com/src/hb/hibernatejar.zip) in WEB-INF folder in eclipse workspace. Still it says hibernate.sourceforge.net is unreachable. I replaced it with www.hibernate.org/dtd and that is also giving same error. Please help me fix this.

Same issue in mapping file as well.

Error after adding classpath in DOCTYPE:

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 StoreData.main(StoreData.java:12)
Caused by: org.dom4j.DocumentException: unknown protocol: classpath Nested exception: unknown protocol: classpath
    at org.dom4j.io.SAXReader.read(SAXReader.java:484)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1481)
    ... 2 more

回答1:

Your DOCTYPE definition refers to a DTD that is on hibernate.sourceforge.net. Now that DTD exists (I can browse it via Firefox) but your application cannot access it. The purpose of the doctype element is to link to a DTD.

Try using below

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"classpath://org/hibernate/hibernate-mapping-3.0.dtd">


回答2:

Following should be your complete DTD in hibernate.cfg.xml file

<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

Also do not forget following at the start in the file

<?xml version='1.0' encoding='utf-8'?>