tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Hibernate > Hibernate Query Language > Create Map using Aggregate Functions

Create Map using Aggregate Functions 

The following example shows creating map from the results of aggregate functions. Refer first example for the configuration and mapping.

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

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

public class HQLMinMaxCountMapTest {

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

    Session session = HibernateUtil.getSessionFactory().openSession();
    String HQL_QUERY = "select new map(max(projectCount) as max_project_count," 
      "min(employeeCount) as min_emp_count, count(*) as company_count) from Company comp";
     * If the above query does'nt return unique result, the
     * 'uniqueResult' method throws an exception.
     *  org.hibernate.NonUniqueResultException: query did not return a unique result: 8
     *  So be sure while using this method.
    Map stats = (Mapsession.createQuery(HQL_QUERY).uniqueResult();    



It gives the following output,
{max_project_count=8478, company_count=10, min_emp_count=478}


bl  br