Hibernate初始化时的Could not parse configuration

本文介绍了解决Hibernate在加载时出现的Could not parse configuration异常的方法,主要原因是hibernate.cfg.xml文件中的DTD路径错误,提供了正确的DTD路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在使用hibernate时,报了 Could not parse configuration: /hibernate.cfg.xml 

这很有可能是hibernate.cfg.xml文件出错

此时可以先用浏览器打开 hibernate.cfg.xml文件 看看能不能正常显示,如果不能,则说明配置文件编写有问题(比如DTD的url错误,标签错误等),改正相应的错误后就不报异常了



问题现象:加载Hibernate时出现异常,可以看到异常信息:

      org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml

      Caused by: org.dom4j.DocumentException: Connection timed out: connect Nested exception: Connection timed out: connect

 

        原因:hibernate.cfg.xml中引用了错误的DTD文件路径“http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd”。

 

        解决方法:替换为正确的DTD路径:“http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd”。



<!--高版本hibernate的hibernate.cfg.xml文件 -->

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

上面红色这一句根据hibernate版本不同会有所不同,有可能需要改成

"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">(低版本hibernate),当然,对应的hbm.xml文件头部也要修改成一样的才行



注意:hibernate.cfg.xml是configuration,*.hbm.xml是mapping

并且二者的地址前半部分一定要一致,要么都是http://hibernate.sourceforge.net/ 要么都是http://hibernate.org/dtd/


注:使用http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd 和 

http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd 不管有网无网都可以!



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值