tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Hibernate > Criteria Queries > Projection List

Projection List 

The following example shows how to use Hibernate projection list API. Refer first example for the configuration and mapping.

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

import java.util.Arrays;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Projections;

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

public class CriteriaProjectionListTest {

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

    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();
    
    Criteria criteria = session.createCriteria(Company.class);
    criteria.setProjection(Projections.projectionList()
            .addProjections.rowCount() )
            .addProjections.avg("employeeCount") )
            .addProjections.max("projectCount") )
      );
    
    Object [] stats = (Object []) criteria.uniqueResult();
    System.out.println(Arrays.toString(stats));
    
    session.getTransaction().commit();
    session.close();  
  }
  
}
   

It gives the following output,
[10, 40294.4, 8478]



 
  


  
bl  br