2

I am not much familiar with Hibernate. Can anyone please help me in resolving error I am getting in my code.

I am using Eclipse Helios, Hibernate 3 & Java 6. I have generated java code from Database tables Emp, Dept using Jboss Hibernate plugin via reverse engineering. Also included all required jar files in project.

Classes and configuration files generated Like:

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 I am getting is:

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 org.hibernate.cfg.Configuration.configure(Configuration.java:1411) at test.EmpDAO.main(EmpDAO.java:19) 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) ... 3 more

Can anyone provide me the description about exception or any suggetion what I am missing here in code?

TimeToCode
  • 901
  • 2
  • 16
  • 34
Ram Soni
  • 31
  • 5
  • Can you update your dtd .Can you see following links http://stackoverflow.com/questions/27358037/exception-in-thread-main-org-hibernate-hibernateexception-could-not-parse-con http://stackoverflow.com/questions/26292287/hibernate-could-not-parse-configuration-hibernate-cfg-xml – Pradeep Sep 21 '16 at 14:34

1 Answers1

1

Thanks Pradeep for your help. Error solved by downloading dtd file "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" and "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd" on my local system and given reference of it. Possibly error was due to access denied by firewall or antivirus on my system to the dtd URL's.

Ram Soni
  • 31
  • 5