|
I am running Emp-Dept one-to- many example with Eclipse 3.2 and hibernate3.2 When I run the client program t insert the record, hibernate is not responding. It is just printing the following message on the console....
Starting ..
Saving data for Emp..
Hibernate: select emp_.empno, emp_.deptno, emp_.sal as sal0_, emp_.loc as loc0_ from EMP emp_ where emp_.empno=? and emp_.deptno=? Hibernate: insert into DEPT (deptname, empno, deptno) values (?, ?, ?)
When I am taking out the relationship its working fine, if somethng is wrong with mapping or somewhere else ... why Hibernate is not complaing abt error rather than sitting quit??? And, thread is ALIVE.
Below is code.
Dept.java
---------------------------------------------------------------
code:
package hello;
import java.util.Set;
public class Dept
{
private String deptname;
private DeptKey key;
private Set emp;
public Set getEmp() {
return emp;
}
public void setEmp(Set emp) {
this.emp = emp;
}
public DeptKey getKey() {
return key;
}
public void setKey(DeptKey key) {
this.key = key;
}
public String getDeptname ()
{
return deptname;
}
public void setDeptname (String deptname)
{
this.deptname = deptname;
}
}
DeptKey.java (Composite PK Class)
---------------------------------------------------------------
code:
package hello;
import java.io.Serializable;
public class DeptKey implements Serializable
{
private String empno;
private String deptno;
public String getDeptno ()
{
return deptno;
}
public void setDeptno (String deptno)
{
this.deptno = deptno;
}
public String getEmpno ()
{
return empno;
}
public void setEmpno (String empno)
{
this.empno = empno;
}
public int hashCode ()
{
return this.hashCode();
}
public boolean equals (Object other)
{
if ( (this == other))
return true;
return false;
}
}
Emp.java
---------------------------------------------------------------
code:
package hello;
public class Emp
{
private EmpKey key;
private String sal;
private String loc;
public EmpKey getKey ()
{
return key;
}
public void setKey (EmpKey key)
{
this.key = key;
}
public String getLoc ()
{
return loc;
}
public void setLoc (String loc)
{
this.loc = loc;
}
public String getSal ()
{
return sal;
}
public void setSal (String sal)
{
this.sal = sal;
}
}
EmpKey.java
---------------------------------------------------------------
code:
package hello;
import java.io.Serializable;
public class EmpKey implements Serializable
{
private String empno;
private String deptno;
public String getDeptno ()
{
return deptno;
}
public void setDeptno (String deptno)
{
this.deptno = deptno;
}
public String getEmpno ()
{
return empno;
}
public void setEmpno (String empno)
{
this.empno = empno;
}
public int hashCode ()
{
return this.hashCode();
}
public boolean equals (Object other)
{
if ( (this == other))
return true;
return false;
}
}
dept.hbm.xml
---------------------------------------------------------------
code:
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="hello">
<class name="Dept" table="DEPT">
<composite-id name="key" class="DeptKey">
<key-property name="empno" type="string"/>
<key-property name="deptno" type="string"/>
</composite-id>
<property name="deptname" type="string"/>
<set name="emp" table="emp" inverse="true" cascade="all" lazy="false">
<key>
<column name="EMPNO" />
<column name="DEPTNO" />
</key>
<one-to-many class="Emp" />
</set>
</class>
</hibernate-mapping>
emp.hbm.xml
---------------------------------------------------------------
code:
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="hello">
<class name="Emp" table="EMP">
<composite-id name="key" class="EmpKey">
<key-property name="empno" type="string"/>
<key-property name="deptno" type="string"/>
</composite-id>
<property name="sal" type="string"/>
<property name="loc" type="string"/>
</class>
</hibernate-mapping>
Client.java
---------------------------------------------------------------
code:
import java.util.HashSet;
import java.util.Set;
import hello.Dept;
import hello.DeptKey;
import hello.Emp;
import hello.EmpKey;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class Client {
private void insertRecord()
{
SessionFactory sessionFactory = new Configuration().configure ().buildSessionFactory ();
Session session = sessionFactory.openSession ();
Transaction tx = session.beginTransaction();
System.out.println("Starting .. ");
tx.begin();
EmpKey empKey = new EmpKey();
empKey.setDeptno("dept1");
empKey.setEmpno("emp1");
Emp emp = new Emp();
emp.setKey(empKey);
emp.setLoc("California");
emp.setSal("1000");
DeptKey deptKey = new DeptKey();
deptKey.setDeptno("dept1");
deptKey.setEmpno("emp1");
Dept dept = new Dept();
dept.setKey(deptKey);
dept.setDeptname("IT");
Set empSet = new HashSet();
empSet.add(emp);
dept.setEmp(empSet);
System.out.println("Saving data for Emp and Dept..");
session.save(dept);
tx.commit();
System.out.println("Transaction committed ..");
}
public static void main(String args[])
{
new Client().insertRecord();
}
}
-----------------------------------------
SQL> desc emp
Name Null? Type
----------------------------------------- -------- ----------------
EMPNO NOT NULL VARCHAR2(10)
DEPTNO NOT NULL VARCHAR2(10)
SAL VARCHAR2(10)
LOC VARCHAR2(10)
Note : empno and deptno is PK there is no FK
SQL> desc dept
Name Null? Type
----------------------------------------- -------- -----------------------
EMPNO NOT NULL VARCHAR2(10)
DEPTNO NOT NULL VARCHAR2(10)
DEPTNAME VARCHAR2(10)
Note : empno and deptno is PK there is no FK
Please let me know, where I am going wrong ????
|