Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate query for selecting multiple values

Tags:

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?

like image 395
user93796 Avatar asked Nov 28 '12 14:11

user93796


People also ask

How can I get multiple records in hibernate?

Session session = HibernateUtil. getCurrentSession(); If you want to fetch the records from a database table try this simple query: List<Object> slist = session.

Can we use select * in HQL?

Keywords like SELECT, FROM, and WHERE, etc., are not case sensitive, but properties like table and column names are case sensitive in HQL.

How many types of queries are there in hibernate?

To create query in the Hibernate ORM framework, there is three different types.


2 Answers

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];          .....      } 
like image 151
Yogendra Singh Avatar answered Sep 24 '22 10:09

Yogendra Singh


You will get a list of arrays of Objects (each one with two elements)

List< Object[] > employees = q.list();  for ( Object[] employee : employees ) {     // employee[0] will contain the first name     // employee[1] will contail the ID } 
like image 31
Matteo Avatar answered Sep 22 '22 10:09

Matteo