tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Hibernate > Hibernate Query Language > Access Properties As List

Access Properties As List 

The following example shows accessing individual properties of entity as list. Each row with matching condition will be returned as a list. Refer first example for the configuration and mapping.

File Name  :  
com/bethecoder/tutorials/hibernate/basic/tests/HQLAccessPropsAsListTest.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.hibernate.basic.tests;

import java.util.Iterator;
import java.util.List;
import org.hibernate.Session;
import com.bethecoder.tutorials.hibernate.basic.util.HibernateUtil;

public class HQLAccessPropsAsListTest {

  /**
   @param args
   */
  public static void main(String[] args) {

    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();
    
    String HQL_QUERY = "select new list(comp.id, comp.name) from Company comp where comp.employeeCount >= 23430";
    List<?> rows = session.createQuery(HQL_QUERY).list();    
    
    System.out.println("Selected row count : " + rows.size());
    Iterator<?> rowsIt = rows.iterator();
    List<?> row = null;
    
    while (rowsIt.hasNext()) {
      row = (List<?>rowsIt.next();
      System.out.println(row + "--> Company Id : " + row.get(0" Company Name : " + row.get(1));
    }
    
    session.getTransaction().commit();
    session.close();
  }

}
   

It gives the following output,
Selected row count : 6
[2, ABC]--> Company Id : 2 Company Name : ABC
[3, MNO]--> Company Id : 3 Company Name : MNO
[4, IJK]--> Company Id : 4 Company Name : IJK
[5, ART]--> Company Id : 5 Company Name : ART
[6, RNK]--> Company Id : 6 Company Name : RNK
[7, AOQ]--> Company Id : 7 Company Name : AOQ



 
  


  
bl  br