org.dom4j.DocumentException:拒绝连接:在Hibernate中连接嵌套异常

时间:2016-09-21 14:12:40

标签: java xml eclipse hibernate

我对Hibernate不熟悉。任何人都可以帮我解决我在代码中遇到的错误。

我正在使用Eclipse Helios,Hibernate 3& Java 6.我已经从数据库表Emp生成了Java代码,Dept通过逆向工程使用Jboss Hibernate插件。 还包括项目中所有必需的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();
    }
}

我得到的例外是:

  

线程中的异常&#34; main&#34; 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)           at test.EmpDAO.main(EmpDAO.java:19)       引起:org.dom4j.DocumentException:连接被拒绝:连接嵌套异常:连接被拒绝:连接           在org.dom4j.io.SAXReader.read(SAXReader.java:484)           在org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1481)           ......还有3个

任何人都可以在代码中向我提供有关异常或任何建议的描述吗?

1 个答案:

答案 0 :(得分:0)

感谢Pradeep的帮助。通过在我的本地系统上下载dtd文件“http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd”和“http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd”并给出参考来解决错误。可能是错误是由于防火墙或我的系统上的防病毒软件拒绝访问dtd URL。