2016-09-21 2 views
1

Я не очень хорошо знаком с Hibernate. Может кто-нибудь, пожалуйста, помогите мне в разрешении ошибки, которую я получаю в своем коде.org.dom4j.DocumentException: Connection failed: connect Nested exception in Hibernate

Я использую Eclipse, Helios, Hibernate 3 & Java 6. Я создал Java-код из таблиц базы данных Emp, Dept с помощью Jboss гибернации плагина с помощью обратного проектирования. Также включены все необходимые файлы jar в проекте.

Классы и конфигурационные файлы генерируются как:

Emp.java

package generatedcode; 
import java.math.BigDecimal; 
import java.util.Date; 
import java.util.HashSet; 
import java.util.Set; 

public class Emp implements java.io.Serializable 
{ 

    private short empno; 
    private Emp emp; 
    private Dept dept; 
    private String ename; 
    private String job; 
    private Date hiredate; 
    private BigDecimal sal; 
    private BigDecimal comm; 
    private Set emps = new HashSet(0); 


    public Emp() { 
    } 

    public Emp(short empno) { 
     this.empno = empno; 
    } 

    public Emp(short empno, Emp emp, Dept dept, String ename, String job, 
      Date hiredate, BigDecimal sal, BigDecimal comm, Set emps) { 
     this.empno = empno; 
     this.emp = emp; 
     this.dept = dept; 
     this.ename = ename; 
     this.job = job; 
     this.hiredate = hiredate; 
     this.sal = sal; 
     this.comm = comm; 
     this.emps = emps; 
    } 

    public short getEmpno() { 
     return this.empno; 
    } 

    public void setEmpno(short empno) { 
     this.empno = empno; 
    } 

    public Emp getEmp() { 
     return this.emp; 
    } 

    public void setEmp(Emp emp) { 
     this.emp = emp; 
    } 

    public Dept getDept() { 
     return this.dept; 
    } 

    public void setDept(Dept dept) { 
     this.dept = dept; 
    } 

    public String getEname() { 
     return this.ename; 
    } 

    public void setEname(String ename) { 
     this.ename = ename; 
    } 

    public String getJob() { 
     return this.job; 
    } 

    public void setJob(String job) { 
     this.job = job; 
    } 

    public Date getHiredate() { 
     return this.hiredate; 
    } 

    public void setHiredate(Date hiredate) { 
     this.hiredate = hiredate; 
    } 

    public BigDecimal getSal() { 
     return this.sal; 
    } 

    public void setSal(BigDecimal sal) { 
     this.sal = sal; 
    } 

    public BigDecimal getComm() { 
     return this.comm; 
    } 

    public void setComm(BigDecimal comm) { 
     this.comm = comm; 
    } 

    public Set getEmps() { 
     return this.emps; 
    } 

    public void setEmps(Set emps) { 
     this.emps = emps; 
    } 

} 

Dept.java

package generatedcode; 

import java.util.HashSet; 
import java.util.Set; 

public class Dept implements java.io.Serializable { 

    private byte deptno; 
    private String dname; 
    private String loc; 
    private Set emps = new HashSet(0); 

    public Dept() { 
    } 

    public Dept(byte deptno) { 
     this.deptno = deptno; 
    } 

    public Dept(byte deptno, String dname, String loc, Set emps) { 
     this.deptno = deptno; 
     this.dname = dname; 
     this.loc = loc; 
     this.emps = emps; 
    } 

    public byte getDeptno() { 
     return this.deptno; 
    } 

    public void setDeptno(byte deptno) { 
     this.deptno = deptno; 
    } 

    public String getDname() { 
     return this.dname; 
    } 

    public void setDname(String dname) { 
     this.dname = dname; 
    } 

    public String getLoc() { 
     return this.loc; 
    } 

    public void setLoc(String loc) { 
     this.loc = loc; 
    } 

    public Set getEmps() { 
     return this.emps; 
    } 

    public void setEmps(Set emps) { 
     this.emps = emps; 
    } 
} 

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.connection.driver_class">oracle.jdbc.driver.OracleDriver</property> 
     <property name="hibernate.connection.url">jdbc:oracle:thin:@myserver:1521:xe</property> 
     <property name="hibernate.connection.username">user</property> 
     <property name="hibernate.connection.password">pass</property> 
     <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property> 
     <mapping resource="Emp.hbm.xml"/> 
     <mapping resource="Dept.hbm.xml"/> 
    </session-factory> 
</hibernate-configuration> 

Emp.hbm.xml

<?xml version="1.0"?> 
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
<!-- Generated Sep 20, 2016 7:06:48 PM by Hibernate Tools 3.4.0.CR1 --> 
<hibernate-mapping> 
    <class name="Emp" table="EMP" schema="TTRAK1"> 
     <id name="empno" type="short"> 
      <column name="EMPNO" precision="4" scale="0" /> 
      <generator class="assigned" /> 
     </id> 
     <many-to-one name="emp" class="Emp" fetch="select"> 
      <column name="MGR" precision="4" scale="0" /> 
     </many-to-one> 
     <many-to-one name="dept" class="Dept" fetch="select"> 
      <column name="DEPTNO" precision="2" scale="0" /> 
     </many-to-one> 
     <property name="ename" type="string"> 
      <column name="ENAME" length="10" /> 
     </property> 
     <property name="job" type="string"> 
      <column name="JOB" length="9" /> 
     </property> 
     <property name="hiredate" type="date"> 
      <column name="HIREDATE" length="7" /> 
     </property> 
     <property name="sal" type="big_decimal"> 
      <column name="SAL" precision="7" /> 
     </property> 
     <property name="comm" type="big_decimal"> 
      <column name="COMM" precision="7" /> 
     </property> 
     <set name="emps" table="EMP" inverse="true" lazy="true" fetch="select"> 
      <key> 
       <column name="MGR" precision="4" scale="0" /> 
      </key> 
      <one-to-many class="Emp" /> 
     </set> 
    </class> 
</hibernate-mapping> 

Dept.hbm.xml

<?xml version="1.0"?> 
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
<!-- Generated Sep 20, 2016 7:06:48 PM by Hibernate Tools 3.4.0.CR1 --> 
<hibernate-mapping> 
    <class name="Dept" table="DEPT" schema="TTRAK1"> 
     <id name="deptno" type="byte"> 
      <column name="DEPTNO" precision="2" scale="0" /> 
      <generator class="assigned" /> 
     </id> 
     <property name="dname" type="string"> 
      <column name="DNAME" length="14" /> 
     </property> 
     <property name="loc" type="string"> 
      <column name="LOC" length="13" /> 
     </property> 
     <set name="emps" table="EMP" inverse="true" lazy="true" fetch="select"> 
      <key> 
       <column name="DEPTNO" precision="2" scale="0" /> 
      </key> 
      <one-to-many class="Emp" /> 
     </set> 
    </class> 
</hibernate-mapping> 

Test.java

package test; 

import generatedcode.Emp; 
import java.util.List; 
import org.hibernate.Query; 
import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.hibernate.cfg.*; 

public class Test 
{ 
    public static void main(String args[]) 
    { 
     Configuration configuration=new Configuration(); 
     SessionFactory sf=configuration.configure().buildSessionFactory(); 

     Session session=sf.openSession(); 
     session.beginTransaction(); 
     Query query=session.createQuery("from Emp"); 
     List<Emp> empList=query.list(); 
     for(Emp emp:empList) 
     { 
      System.out.println("Employee "+emp.getEmpno()+" , "+emp.getEname()); 
     } 
     session.close(); 
    } 
} 

Exception я получаю:

Исключение в потоке "основного" org.hibernate.HibernateException: Невозможно разобрать конфигурацию: /hibernate.cfg.xml в org.hibernate.cfg.Configuration.doConfigure (Configuration.java:1491) на org.hibernate.cfg.Configuration.configure (Configuration.java:1425) на org.hibernate.cfg.Configuration.configure (Configuration.java:1411) на test.EmpDAO.main (EmpDAO.java:19) Вызвано: org.dom4j.DocumentException: соединение отклонено: connect Вложенное исключение: соединение отклонено: подключитесь на org.dom4j.io.SAXReader.read (SAXReader .java: 484) в org.hibernate.cfg.Configuration.doConfigure (Configuration.java:1481) ... еще 3

Может кто-нибудь предоставить мне описание исключения или любого suggetion что я отсутствующий здесь в коде?

+0

Вы можете обновить ОТД .Can вы видите следующие ссылки http://stackoverflow.com/questions/27358037/exception-in-thread-main-org-hibernate-hibernateexception-could-not-parse-con Http: // StackOverflow.ком/вопросы/26292287/спящий режим, может-не-синтаксический-конфигурация-зимуют-CFG-XML – Pradeep

ответ

0

Поблагодарите Pradeep за помощью. Ошибка решена путем загрузки файла dtd «http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd» и «http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd» в моей локальной системе и с его ссылкой. Вероятно, ошибка связана с тем, что доступ к файлу-брандмауэру или антивирусу в моей системе запрещен URL-адресам dtd.

Смежные вопросы