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

Access Properties As Map 

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

File Name  :  
com/bethecoder/tutorials/hibernate/basic/tests/HQLAccessPropsAsMapTest.java 
Author  :  Sudhakar KV
Email  :  kvenkatasudhakar@gmail.com
   
package com.bethecoder.tutorials.hibernate.basic.tests;

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

public class HQLAccessPropsAsMapTest {

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

    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();
    
    String HQL_QUERY = 
      "select new map(comp.id as company_id, comp.name as company_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();
    Map row = null;
    
    while (rowsIt.hasNext()) {
      row = (MaprowsIt.next();
      System.out.println(row);
    }
    
    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}


                    Print

   



Books Recommendation

 
 
bl  br