-->
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.  [ 2 posts ] 
Author Message
 Post subject: how to get values
PostPosted: Fri Jul 27, 2007 11:50 pm 
Newbie

Joined: Fri Jul 27, 2007 3:42 am
Posts: 6
i have to tables i am firing following query

session.createQuery( "Select Employee.empid,Employee.surName,salary.empid from Employee Employee,salary salary Where salary.empid=Employee.empid" )

now how to reterive values?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 28, 2007 10:01 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
The query will return tuples of object (more detail in hibernate docs). Each tuple is an array of objects containined the values of the selected fields in the order they appear in the select list.

e.g.
Code:
Query q = session.createQuery( "Select Employee.empid,Employee.surName,salary.empid from Employee Employee,salary salary Where salary.empid=Employee.empid" );
Iterator results = q.list().iterator;
while (results.hasNext() {
  Object[] tuple = (Object[])results.next();
  Long empid = (Long)tuple[0];
  String surname = (String)tuple[1];
  Integer salary = (Integer)[2];
}

Obviously the types must match the data returned in your case.

An elegant alternative to this is to create a specific bean to hold the properties you're requesting. You can instruct hibernate to create and populate the bean in the query. The query result is then a list of these beans.

e.g.
Query:
"Select new com.xx.EmployeeBasicInfo(Employee.empid, Employee.surName, salary.empid) from Employee Employee,salary salary Where salary.empid=Employee.empid

Bean:
Code:
public class EmployeeBasicInfo {

   private Long employeeId;
   private String surname;
   private int salary;
   
   public EmployeeBasicInfo(Long employeeId, String surname, int salary) {
      super();
      this.employeeId = employeeId;
      this.surname = surname;
      this.salary = salary;
   }
   public Long getEmployeeId() {
      return employeeId;
   }
   public String getSurname() {
      return surname;
   }
   public int getSalary() {
      return salary;
   }
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.