0

I would like to know what below HQL statement returns and how to iterate it,

String hql0="select distinct d.dept_name as Dept_Name "+
            " from com.gavs.beans.Department as d";

org.hibernate.query.Query q0 = session.createQuery(hql0);
List r0=q0.list();

Now I couldn't find how to iterate this list using for loop. This is my problem. Thank You.

vishnu
  • 195
  • 1
  • 3
  • 19
  • 2
    have you tried using foreach loop? `for(Object obj: r0){}` – Finci Jun 10 '20 at 08:21
  • No. Can you give me the the complete statement of for(Object obj: ro){...} – vishnu Jun 10 '20 at 08:29
  • For example, is it like this for (Object[] result : results) { Integer maxPrice = (Integer)result[0]; Integer minPrice = (Integer)result[1]; Long count = (Long)result[2]; } – vishnu Jun 10 '20 at 08:31
  • 1
    Not really sure what your question is. You are asking how to iterate on a List ? That's not at all `hibernate` or `hql` related, it has nothing to do with `ResultSet` either. So is your question really "I have a `List`, how can I iterate on its content in Java" ? https://stackoverflow.com/questions/18410035/ways-to-iterate-over-a-list-in-java – GPI Jun 10 '20 at 08:31
  • Yes, you are correct. – vishnu Jun 10 '20 at 08:33
  • Hi Finci, can you please tell me how to get the dept_name using , for(Object obj:r0)?@Finci – vishnu Jun 10 '20 at 09:47

1 Answers1

0

I iterated the list as shown below,


String hql0="select distinct d.dept_name as Dept_Name "+
            " from com.gavs.beans.Department as d";

org.hibernate.query.Query q0 = session.createQuery(hql0);
List r0=q0.getResultlist(); //Instead of using list(), I used getResultList()

for(Object o:r0)
{
System.out.println(o.toString());
}

This will print all the department names of the entity Department.

vishnu
  • 195
  • 1
  • 3
  • 19