-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Facing problem in cascading update
PostPosted: Fri Nov 07, 2003 1:25 am 
Newbie

Joined: Tue Oct 28, 2003 2:20 am
Posts: 9
Location: Bangalore, India
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.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 07, 2003 6:04 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Code:
<many-to-one name="department" class="com.infosys.j2ee.cmptest.model.Department" column="departmentid" cascade="all-delete-orphan"/>


Should be
Code:
<many-to-one name="department" class="com.infosys.j2ee.cmptest.model.Department" column="departmentid" />


Plus all-delete-orphan from many-to-one is quite strange and probably means nothing.

Here is the sequence
* remove 1 employee from dep
* this employee is deleted
* cascade delete from employee to department

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 07, 2003 10:12 am 
Newbie

Joined: Tue Oct 28, 2003 2:20 am
Posts: 9
Location: Bangalore, India
Thanks. That worked.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.