Search code examples
javahibernatehql

How to store a resultSet in the list and iterate a list with properties of an entity in a HQL statement?


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.


Solution

  • 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.