I have one-to-many relationship Department-has-Employees. See details in
http://forum.hibernate.org/viewtopic.php?p=2177261#2177261
I am trying to increase the salaries of all the employees for a department like this
Code:
public void increaseSalaryForLocation(Integer id, double raise) {
Session session = null;
try {
session = sf.openSession();
Query query = session.createQuery("from Department as dep left outer join fetch dep.employees where dep.id = :id");
query.setParameter("id",id);
Department dep = (Department)query.list().get(0);
for(Iterator j=dep.getEmployees().iterator();j.hasNext();) {
Employee emp = (Employee)j.next();
double salary = emp.getSalary();
salary += raise;
emp.setSalary(salary);
}
session.flush();
} catch (Exception e){
System.out.println("Error in bulk insert of dept "+e.getMessage());
e.printStackTrace();
} finally {
try {
session.close();
} catch (Exception e) {
System.out.println("Hibernate Exception " + e.getMessage());
e.printStackTrace();
}
}
}
Strangely each employees salary is increased by
raise*(the number of employees in the department. For e.g. if there are 10 employees in the department and the raise is specified as 1 then each employees salary is raised by 10 instead of 1.
What am I doing wrong?