为什么不会hibernate配置文件解析

时间:2014-09-03 13:34:49

标签: java hibernate

我正在eclipse上构建我的第一个hibernate应用程序并从Oracle表中读取一组SFTP凭据,因为我想让它们可配置而不是硬编码。但是当我尝试运行应用程序时,它不会解析hibernate.cfg.xml文件。我尝试输入所有配置文件而不是剪切和粘贴,我尝试将配置文件中的www.hibernate.org/dtd/hibernate-configuration-3.0.dtd更改为http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd。为什么要赢得这项工作呢?

这是控制台输出;

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 name.of.package.used.retrieve.main(retrieve.java:14)
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

这是映射文件;

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
                               "http://hibernate.sourceforge.net/hibernate-mapping   
3.0.dtd">
<hibernate-mapping>
     <class name="name.of.package.used.SFTPDetails" table="SFTP_Creds">
          <id column="SFTP_HOST" name="sftp_host">
               <generator class="assigned" />
           </id>
          <property column="SFTP_PORT" generated="never" lazy="false"
                name="sftp_port" />
          <property column="SFTP_USERNAME" generated="never" lazy="false"
                name="sftp_username" />
          <property column="SFTP_PASSWORD" generated="never" lazy="false"
                name="sftp_password" />
          <property column="SFTP_HOSTKEY" generated="never" lazy="false"
                name="sftp_hostkey" />
    </class>
</hibernate-mapping>

这是 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-configuration-3.0.dtd">

<hibernate-configuration>
   <session-factory>
     <property name="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</property>
     <property name="connection.url">jdbc:oracle:thin:@hostname:1523:TRNG</property>
     <property name="hibernate.connection.username">username</property>
     <property name="hibernate.connection.password">password</property>
     <property   
    name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
      <mapping resource="sftpcreds.hbm.xml"/>
  </session-factory>
</hibernate-configuration>

我运行应用程序的主要Java文件是;

package name.of.package.used;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.mapping.List;
import org.apache.log4j.Logger;

public class retrieve {
   public static void main(String args[]) {
     Configuration cfg = new Configuration();
     cfg.configure("hibernate.cfg.xml");// populates the data of the configuration file

    // creating seession factory object
    SessionFactory factory = cfg.buildSessionFactory();

    // creating session object
    Session session = factory.openSession();

    // creating transaction object
    Transaction t = session.beginTransaction();

    Query query = session.createQuery("from SFTPCreds");
    java.util.List list = query.list();
    System.out.println(list);
    t.commit();
    session.close();
 }
}

2 个答案:

答案 0 :(得分:0)

我猜这是你的dtd参考:http://hibernate.sourceforge.net/hibernate-mapping
3.0.dtd

这已经过时和过时你应该使用ttp://www.jboss.org/dtd/hibernate/hibernate-configuration-3.0.dtd

答案 1 :(得分:0)

这意味着无法解析hibernate.dtd - 在服务器上尝试解析它。 dtd包含在jars文件中 - 有关如何解决问题,请参阅herehere

还可以尝试将路径从hibernate.org/dtd更改为hibernate.sourceforge.net

这意味着XML解析器在尝试访问DTD定义时遇到问题。因此,首先要做的是查看是否可以直接浏览网址并检查代理设置。就我而言,我改变了它

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

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

我认为您也可以尝试使用“http://www.jboss.org/dtd/hibernate/hibernate-configuration-3.0.dtd”。似乎hibernate.org无论如何都要转向jboss位置。