无法解析配置:hibernate.cfg.xml错误

时间:2014-11-03 11:41:26

标签: java xml hibernate

在hibernate中获得以下异常

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

Hibernate配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
    <property name="hbm2ddl.auto">create</property> 
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/platinum</property>
    <property name="hibernate.connection.username">admin</property>
    <property name="hibernate.connection.password">admin</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 

    <mapping resource="employee.hbm.xml" />
</session-factory>
</hibernate-configuration>

有什么建议吗?

2 个答案:

答案 0 :(得分:1)

这是一个临时解决方案。您的Hibernate jar包含用于验证配置xml的dtd。解压缩'hibernate-configuration-3.0.dtd'并将其放在项目结构的某个目录中(在本例中,我将它放在Project根目录中)。将您的dtd位置添加到DOCTYPE声明。

<!DOCTYPE hibernate-configuration SYSTEM
"hibernate-configuration-3.0.dtd">

它对我有用。它在系统脱机时有效。从本地系统获取DTD。

只是我们必须想办法从你的jar中获取dtd。

你可以这样做:

<!DOCTYPE hibernate-configuration SYSTEM 
    "classpath://org/hibernate/hibernate-configuration-3.0.dtd">

然后,它正在抛出

Caused by: org.dom4j.DocumentException: unknown protocol: classpath Nested exception: unknown protocol: classpath

我收到连接超时错误。你的是连接拒绝错误。如果'hibernate.org'拒绝连接,请尝试此操作。

答案 1 :(得分:0)

在Eclipse Java Project中,使用构建路径添加hibernate jar文件。在hibernate.cfg.xml(配置文件)中添加以下给定的DOCTYPE      同样在DOCTYPE之后的映射文件中(另存为example.hbm.xml-map类与数据库中的表)