tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 
 Hibernate > Criteria Queries > Order using Property

Order using Property 

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

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

import java.util.List;

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

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

public class CriteriaPropOrderTest {

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

    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();
    
    Criteria criteria = session.createCriteria(Company.class);
    criteria.addOrder(Property.forName("name").asc());
    criteria.addOrder(Property.forName("projectCount").desc());
    
    List<Company> companies = criteria.list();    
    System.out.println("Selected row count : " + companies.size());
    
    for (Company company : companies) {
      System.out.println(company);
    }
    
    session.getTransaction().commit();
    session.close();
  }
  
}
   

It gives the following output,
Selected row count : 10
{ id = 2, name = ABC, employeeCount = 23430, 
	projectCount = 160, address1 = add11, address2 = add22 }
{ id = 7, name = AOQ, employeeCount = 97890, 
	projectCount = 4578, address1 = add17892, address2 = add2893 }
{ id = 5, name = ART, employeeCount = 67890, 
		projectCount = 850, address1 = add1112, address2 = add2223 }
{ id = 8, name = BQO, employeeCount = 9778, 
	projectCount = 487, address1 = add1459, address2 = add2443 }
{ id = 9, name = CFR, employeeCount = 478, 
	projectCount = 2487, address1 = null, address2 = add2443 }
{ id = 10, name = CFR, employeeCount = 6478, 
	projectCount = 887, address1 = , address2 = add24435 }
{ id = 4, name = IJK, employeeCount = 67890, 
	projectCount = 850, address1 = add1112, address2 = add2223 }
{ id = 3, name = MNO, employeeCount = 26790, 
	projectCount = 670, address1 = add111, address2 = add222 }
{ id = 1, name = PQR, employeeCount = 3430, 
	projectCount = 220, address1 = add1@, address2 = add2 }
{ id = 6, name = RNK, employeeCount = 98890, 
	projectCount = 8478, address1 = add1412, address2 = add27823 }


                    Print

   



Books Recommendation

 
 
bl  br