-->
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: updating child in 1-to-many
PostPosted: Fri Nov 07, 2003 10:19 am 
Newbie

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


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 07, 2003 11:23 am 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
I think you need to add "select distinct dep " at the beginning of your query


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 07, 2003 11:39 am 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
Btw, if I understand correctly, you need to iterate all employees from some department. Right? But then why do you need to select departments at all?

session = sf.openSession();
Query query = session.createQuery("from Employee as emp where emp.department = :dep");
query.setEntity("dep",dep);
for(Iterator j=query.list().iterator();j.hasNext();) {
Employee emp = (Employee)j.next();
double salary = emp.getSalary();
salary += raise;
emp.setSalary(salary);
}
session.flush();

In this scenario you need Department object to use it as condition (emp.department = :dep). You may have it already. If not - you can easily construct new one - only Id property must be initialized.

Department dep = new Department();
dep.setId( ... );


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.