Hibernate query for selecting multiple values

user93796 picture user93796 · Nov 28, 2012 · Viewed 77.3k times · Source

In hibernate I can do following

Query q = session.createQuery("from Employee as e);
List<Employee> emps = q.list();

Now if I want to fetch int and String how can I do it?

Query q = session.createQuery(""SELECT E.firstName,E.ID FROM Employee E";
List ans = q.list();

Now what will be the structure of list?

Answer

Yogendra Singh picture Yogendra Singh · Nov 28, 2012

This is fine. Only thing you need to understand is that it will return list of Object [] as below:

     Query q = session.createQuery("select e.id, e.firstName from Employee e");
     List<Object[]> employees= (List<Object[]>)q.list();
     for(Object[] employee: employees){
         Integer id = (Integer)employee[0];
         String firstName = (String)employee[1];
         .....
     }