关于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的头是不一样的。
如果你真心觉得文章写得不错,而且对你有所帮助,那就不妨小小打赏一下吧,如果囊中羞涩,不妨帮忙“推荐"一下,您的“推荐”和”打赏“将是我最大的写作动力!
本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接.