tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Hibernate > Hibernate Query Language > Top Five Employed Companies

Top Five Employed Companies 

The following example shows getting top five employed companies. Refer first example for the configuration and mapping.

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

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

public class TopListTest {

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

    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();
    String HQL_QUERY = "from Company comp where 5 > " +
        "(select count(*) from Company comp2 where comp2.employeeCount > comp.employeeCount)" 
        " order by comp.employeeCount desc";
    List<Company> companies = (List<Company>session.createQuery(HQL_QUERY).list();    
    
    for (Company company : companies) {
      System.out.println(company);
    }
    
    session.getTransaction().commit();
    session.close();
  }

}
   

It gives the following output,
{ id = 6, name = RNK, employeeCount = 98890, 
	projectCount = 8478, address1 = add1412, address2 = add27823 }
{ id = 7, name = AOQ, employeeCount = 97890, 
	projectCount = 4578, address1 = add17892, address2 = add2893 }
{ id = 4, name = IJK, employeeCount = 67890, 
	projectCount = 850, address1 = add1112, address2 = add2223 }
{ id = 5, name = ART, employeeCount = 67890, 
	projectCount = 850, address1 = add1112, address2 = add2223 }
{ id = 3, name = MNO, employeeCount = 26790, 
	projectCount = 670, address1 = add111, address2 = add222 }



 
  


  
bl  br