关于Could not parse configuration: /hibernate.cfg.xml的问题

  第一次在eclipse上配置hibernate,问题百出啊,比如下面的org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml问题,知道是

hibernate.cfg.xml配置问题解决有问题,但不知道问题在哪,从Oracle的数据库的链接到po代码,各种找啊。

 1 log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
 2 log4j:WARN Please initialize the log4j system properly.
 3 org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml
 4     at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1376)
 5     at org.hibernate.cfg.Configuration.configure(Configuration.java:1310)
 6     at org.hibernate.cfg.Configuration.configure(Configuration.java:1296)
 7     at test.Query.main(Query.java:15)
 8 Caused by: org.dom4j.DocumentException: Connection timed out: connect Nested exception: Connection timed out: connect
 9     at org.dom4j.io.SAXReader.read(SAXReader.java:484)
10     at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1366)
11     ... 3 more
12 Exception in thread "main" java.lang.NullPointerException
13     at test.Query.main(Query.java:20)

下面是Oracle的hibernate的数据库连接配置(/hibernate.cfg.xml):

 1 <hibernate-configuration>
 2     <session-factory>
 3         <property name="show_sql">true</property>
 4         <property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
 5         <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
 6         <property name="hibernate.connection.url">jdbc:oracle:thin:@Ip地址</property>
 7         <property name="hibernate.connection.username">用户名</property>
 8         <property name="hibernate.connection.password">密码</property>
 9         <mapping resource="com/po/Policy.hbm.xml"></mapping>
10     </session-factory>
11 </hibernate-configuration> 

下面是po的配置(/po.hbm.xml):

 1 <hibernate-mapping package="com.po">
 2     <class name="Policy" table="T_policy_general">
 3         <id name="POLICY_ID" column="POLICY_ID">
 4             <generator class="assigned" />
 5         </id>
 6         <property name="PRODUCT_ID" column="PRODUCT_ID" />
 7         <property name="STATUS_ID" column="STATUS_ID" />
 8         <property name="POLICY_NO" column="POLICY_NO" />
 9     </class>
10 </hibernate-mapping>  

最后发现问题居然是xml文件头写的不对:

网上的错误写法:

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

正确写法:

  /hibernate.cfg.xml
1 <?xml version=‘1.0‘ encoding=‘UTF-8‘?>
2 <!DOCTYPE hibernate-configuration PUBLIC
3           "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
4           "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

  /po.hbm.xml

1 <?xml version=‘1.0‘ encoding=‘UTF-8‘?>
2 <!DOCTYPE hibernate-mapping PUBLIC
3           "-//Hibernate/hibernate-Mapping DTD 3.0//EN"
4           "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

最后成功查询到数据.

总结:不同xml的头是不一样的。

时间: 2024-08-04 04:06:40

关于Could not parse configuration: /hibernate.cfg.xml的问题的相关文章

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

org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1542) at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:1035) at

[Classpath]: Could not parse configuration: ……\hibernate.cfg.xml

解决方法: 将hibernate.cfg.xml 的开头改为: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configur

解决离线Could not parse configuration:hibernate.cfg.xml错误

离线使用hibernate tool 生成反向工程,在配置 配置文件完,生成配置文件后,会报出org.hibernate.HibernateException: Could not parse configuration:错误 原因:就是读取不到本地dtd约束,然后必须每次联网加载才行,具体原因不了解,因为网络这方面的解答都比较模糊,应该是小范围问题,感觉是jdk与tools要求版本不一致,导致部分插件未正常安装,解析出现问题 解决方式(同理可解决映射文件出现的解析不成的问题) : 首先确认cf

hibernate 不能解析hibernate.cfg.xml 文件

1.hibernate解析hibernate.cfg.xml 文件时出现如下错误. org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1542)    at org.hibernate.cfg.Configuration.configure(C

hibernate.cfg.xml配置文件和hbm.xml配置文件 模板

hibernate.cfg.xml配置文件格式 <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration

hibernate3配置文件hibernate.cfg.xml的详细解释

<!--标准的XML文件的起始行,version='1.0'表明XML的版本,encoding='gb2312'表明XML文件的编码方式-->                 <?xml version='1.0' encoding='gb2312'?> <!--表明解析本XML文件的DTD文档位置,DTD是Document Type Definition 的缩写,即文档类型的定义,XML解析器使用DTD文档来检查XML文件的合法性.hibernate.sourceforge

Hibernate配置文件——hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <

java:Hibernate框架(环境搭建,Hibernate.cfg.xml中属性含义,Hibernate常用API对象,HibernteUitl,对象生命周期图,数据对象的三种状态)

1.环境搭建: 三个准备+7个步骤 准备1:新建项目并添加hibernate依赖的jar文件  准备2:在classpath下(src目录下)新建hibernate的配置文件:hibernate.cfg.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configurati

spring applicationContext.xml和hibernate.cfg.xml设置

applicationContext.xml配置 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://ww