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

Least Five Employed Companies 

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

File Name  :  
com/bethecoder/tutorials/hibernate/basic/tests/LeastListTest.java 
Author  :  Sudhakar KV
Email  :  kvenkatasudhakar@gmail.com
   
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 LeastListTest {

  /**
   @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";
    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 = 9, name = CFR, employeeCount = 478, 
	projectCount = 2487, address1 = null, address2 = add2443 }
{ id = 1, name = PQR, employeeCount = 3430, 
	projectCount = 220, address1 = add1@, address2 = add2 }
{ id = 10, name = CFR, employeeCount = 6478, 
	projectCount = 887, address1 = , address2 = add24435 }
{ id = 8, name = BQO, employeeCount = 9778, 
	projectCount = 487, address1 = add1459, address2 = add2443 }
{ id = 2, name = ABC, employeeCount = 23430, 
	projectCount = 160, address1 = add11, address2 = add22 }


                    Print

   



Books Recommendation

 
 
bl  br