Hi,
I have a 1-to-many relationship Department-has-Employees. I have problem in updating the department.
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="com.infosys.j2ee.cmptest.model.Department" table="DepartmentEJBTable">
<id name="primaryKey">
<generator class="sequence">
<param name="sequence">test2_seq</param>
</generator>
</id>
<property name="name">
<column name="NAME"/>
</property>
<property name="location">
<column name="LOCATION"/>
</property>
<bag name="employees" inverse="true" outer-join="true" cascade="all-delete-orphan">
<key column="departmentid"/>
<one-to-many class="com.infosys.j2ee.cmptest.model.Employee"/>
</bag>
</class>
<class name="com.infosys.j2ee.cmptest.model.Employee" table="EmployeeEJBTable">
<id name="primaryKey">
<generator class="sequence">
<param name="sequence">test2_seq</param>
</generator>
</id>
<property name="name">
<column name="NAME"/>
</property>
<property name="employeeNumber">
<column name="EMPLOYEENUMBER"/>
</property>
<property name="hireDate">
<column name="HIREDATE"/>
</property>
<property name="salary">
<column name="SALARY"/>
</property>
<many-to-one name="department" class="com.infosys.j2ee.cmptest.model.Department" column="departmentid" cascade="all-delete-orphan"/>
</class>
</hibernate-mapping>
package com.infosys.j2ee.cmptest.model;
import java.util.ArrayList;
import java.util.Collection;
public class Department implements java.io.Serializable {
private String name;
private String location;
private Collection employees;
private Integer primaryKey;
public Department() {
employees = new ArrayList();
}
public Department(String name, String location, Collection employees) {
this.name = name;
this.location = location;
this.employees = employees;
}
public Collection getEmployees() {
return employees;
}
public String getLocation() {
return location;
}
public String getName() {
return name;
}
public Integer getPrimaryKey() {
return primaryKey;
}
public void setEmployees(Collection collection) {
employees = collection;
}
public void setLocation(String string) {
location = string;
}
public void setName(String string) {
name = string;
}
public void setPrimaryKey(Integer integer) {
primaryKey = integer;
}
public void addEmployee(Employee e) {
this.employees.add(e);
}
}
package com.infosys.j2ee.cmptest.model;
import java.sql.Date;
public class Employee implements java.io.Serializable {
private String name;
private String employeeNumber;
private double salary;
private Date hireDate;
private Integer primaryKey;
private Department department;
public Employee() {}
public Employee(String name, String number, double sal, Date hDate) {
this.name = name;
this.employeeNumber = number;
this.salary = sal;
this.hireDate = hDate;
}
public Department getDepartment() {return this.department;}
public void setDepartment(Department dep) {this.department = dep;}
public String getEmployeeNumber() {return employeeNumber;}
public Date getHireDate() {return hireDate;}
public String getName() {return name;}
public double getSalary() {return salary;}
public void setEmployeeNumber(String string) {employeeNumber = string;}
public void setHireDate(Date date) {hireDate = date;}
public void setName(String string) {name = string;}
public void setSalary(double d) {salary = d;}
public Integer getPrimaryKey() {return primaryKey;}
public void setPrimaryKey(Integer integer) {primaryKey = integer;}
}
In my first URL request I find the department by calling DepartmentHibernateDAO.
Code:
public Department findDepartment(Integer id){
Session session = null;
Department department = null;
try {
session = sf.openSession();
Query q = session.createQuery("from Department as dep left outer join fetch dep.employees where dep.id = :id");
q.setParameter("id",id);
department = (Department)q.list().get(0);
} catch (Exception e) {
System.out.println("Error in findDepartment " + e.getMessage());
e.printStackTrace();
} finally {
try {
session.close();
} catch (Exception e) {
System.out.println("Hibernate Exception " + e.getMessage());
e.printStackTrace();
}
}
return department;
}
I hold the department in the HttpSession and display the department to the web user. When the user submits after updating the department (adding, updating and removing the employees) I then set properties of the Department and then add and remove employees in the second URL request by retreiving the department from the HttpSession :
department.addEmployee(e)
....
department.getEmployees().remove(e).
I then call the DepartmentHibernateDAO.update
Code:
public void update(Integer id, Department department){
Session session = null;
ArrayList removedEmployees = new ArrayList();
try {
session = sf.openSession();
session.update(department, id);
session.flush();
} catch (Exception e) {
System.out.println("Error in update " + e.getMessage());
e.printStackTrace();
} finally {
try {
session.close();
} catch (Exception e) {
System.out.println("Hibernate Exception " + e.getMessage());
e.printStackTrace();
}
}
}
For some reason the department itself gets deleted. I used P6Spy to check out the SQLs that get fired and here are the SQLs fired (in order)
- select test2_seq.nextval from dual
- insert into EmployeeEJBTable (NAME, EMPLOYEENUMBER, HIREDATE, SALARY, departmentid, primaryKey) values ('RamNewNew',23, '2003-01-01', 1000.0, 1216, 1219)
- update DepartmentEJBTable set NAME='Winter', LOCATION='alfalfa' where primaryKey = 1216
- update EmployeeEJBTable set NAME = 'NewUpdate4', EMPLOYEENUMBER = '23', HIREDATE = '2003-01-01', SALARY=14403.0, departmentid = 1216 where primaryKey = 1217
- delete from EmployeeEJBTable where primaryKey = 1219
-
delete from DepartmentEJBTable where primaryKey = 1216
- commit
The delete of Depart... is puzzling! What am I doing wrong?
Regards,
Ram.