HibernateException"无法解析配置:hibernate.cfg.xml"运行简单的Hibernate应用程序时出错

时间:2015-09-21 09:11:52

标签: java hibernate hibernate-mapping

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?

1 个答案:

答案 0 :(得分:0)

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

假设您没有使用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">