tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 
 Hibernate > Hibernate Query Language > Custom Result Transformer

Custom Result Transformer 

The following example shows how to use custom result transformer. Refer first example for the configuration and mapping.

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

import java.util.Collections;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.transform.ResultTransformer;

import com.bethecoder.tutorials.hibernate.basic.CompanyShortInfo;
import com.bethecoder.tutorials.hibernate.basic.util.HibernateUtil;

public class HQLTransformCustomTest {

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

    getCompanies(new SimpleResultTransformer(true));
    getCompanies(new SimpleResultTransformer(false));
  }
  
  public static void getCompanies(SimpleResultTransformer transformer) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();
    
    String HQL_QUERY = "select comp.id as comp_id, comp.name as comp_name " +
              "from Company comp where comp.id < 5";
    Query query = session.createQuery(HQL_QUERY);
    query.setResultTransformer(transformer);
    
    List<?> rows = query.list();    
    System.out.println("Selected row count : " + rows.size());
    
    for (Object row : rows) {
      System.out.println(row);  //Each row is a list of properties in the query
    }
    
    session.getTransaction().commit();
    session.close();
  }
}

class SimpleResultTransformer implements ResultTransformer {

  private static final long serialVersionUID = 1L;
  private boolean reverseOrder = false;
  
  public SimpleResultTransformer(boolean reverseOrder) {
    this.reverseOrder = reverseOrder;
  }
  
  /**
   * Create an object out of each row data.
   * In this scenario we have created a company short info
   * object from row data.
   */
  @Override
  public Object transformTuple(Object[] rowData, String[] aliasNames) {
    Integer compId = (IntegerrowData[0];
    String compName = (StringrowData[1];
    
    CompanyShortInfo shortInfo =  new CompanyShortInfo(compId, compName);
    return shortInfo;
  }
  
  /**
   * Final result list transformation.
   */
  @Override
  public List transformList(List paramList) {
    
    if (reverseOrder) {
      Collections.reverse(paramList);
    }
    
    return paramList;
  }
}
   

It gives the following output,
Selected row count : 4
CompanyShortInfo[4, IJK]
CompanyShortInfo[3, MNO]
CompanyShortInfo[2, ABC]
CompanyShortInfo[1, PQR]

Selected row count : 4
CompanyShortInfo[1, PQR]
CompanyShortInfo[2, ABC]
CompanyShortInfo[3, MNO]
CompanyShortInfo[4, IJK]


                    Print

   



Books Recommendation

 
 
bl  br