2016-09-21 85 views
1

我對Hibernate並不熟悉。任何人都可以幫助我解決我在代碼中遇到的錯誤。org.dom4j.DocumentException:連接被拒絕:連接休眠中的嵌套異常

我正在使用Eclipse Helios,Hibernate 3 & Java 6.我已經從Database Tables 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(); 
    } 
} 

例外,我得到的是:在線程 「主要」 org.hibernate.HibernateException

例外:在org.hibernate.cfg.Configuration.doConfigure /hibernate.cfg.xml (:無法解析配置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:連接被拒絕:連接嵌套異常:連接被拒絕:連接 at org.dom4j.io.SAXReader.read(SAXReader的.java:484) 在org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1481) ... 3個

任何人都可以向我提供有關異常的說明或任何suggetion我錯過了什麼在這裏代碼?

+0

您可以更新您的DTD。可你看以下鏈接 http://stackoverflow.com/questions/27358037/exception-in-thread-main-org-hibernate-hibernateexception-could-not-parse-con HTTP://計算器。COM /問題/ 26292287 /休眠-可能 - 不解析配置 - 冬眠-CFG-XML – Pradeep

回答

相關問題