Hibernate HQL, how to retrieve selected fields

Below example code shows how to retrieve selected fields from database. As shown below a List of Object[] is being returned, by session.query(“SELECT Y.firstName, Y.lastName FROM Employee Y WHERE Y.salary >9000”);

Then List need to be iterated, in which each element is an Object array of size 2(since only two fields are being selected). You can download full NetBeans project sour code here.

import org.hibernate.HibernateException; 
import org.hibernate.Transaction;
import org.hibernate.Session;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HQLManageEmployee {
   private static SessionFactory factory; 
   public static void main(String[] args) {
          //step 1: Create Session Factory
         factory = new Configuration().configure().buildSessionFactory();
      }catch (Throwable ex) { 
         System.err.println("Failed to create sessionFactory object." + ex);
         throw new ExceptionInInitializerError(ex); 
      HQLManageEmployee ME = new HQLManageEmployee();

      /* List down all the employees */

   /* Method to  READ all the employees */
   public void listEmployees( ){
      Session session = factory.openSession();
      Transaction tx = null;
         tx = session.beginTransaction();

         Query qry = session.createQuery("SELECT Y.firstName, Y.lastName FROM Employee Y WHERE Y.salary >9000");

          List<Object []> employees = qry.list(); 
         for (Iterator iterator = 
                           employees.iterator(); iterator.hasNext();){
            Object [] emp =  (Object [])iterator.next(); 

            System.out.println(emp[0]+" --- "+emp[1]);
      }catch (HibernateException e) {
         if (tx!=null) tx.rollback();
      }finally {

Example HibernateHQL