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 Array

Access Properties As Array 

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

File Name  :  
com/bethecoder/tutorials/hibernate/basic/tests/HQLAccessPropsAsArrayTest.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 HQLAccessPropsAsArrayTest {

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

    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();
    
    String HQL_QUERY = "select 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();
    Object [] row = null;
    
    while (rowsIt.hasNext()) {
      row = (Object []) rowsIt.next();
      System.out.println("Company Id : " + row[0" Company Name : " + row[1]);
    }
    
    session.getTransaction().commit();
    session.close();
  }

}
   

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



 
  


  
bl  br