运行简单的休眠应用程序时出现休眠异常"Could not parse configuration: hibernate.cfg.xml"错误


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 refused: connect Nested exception: Connection refused: connect
    at org.dom4j.io.SAXReader.read(SAXReader.java:484)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1481)
    ... 2 more

为什么会出现这个HibernateException ?

这很可能是因为引用了hibernate.cfg.xml顶部的DTD,而该DTD不存在于您正在使用的Hibernate jar中。

假设您使用的不是Hibernate 2.0版本,请检查在Hibernate .cfg.xml中顶部是否有:

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

如果你使用XML hbm文件进行实体映射那么在这些XML文件中最上面有:

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

最新更新