HQL Order By Example


Order by clause is used to retrieve the data from database in the sorted order by any property of returned class or components. HQL supports Order By Clause. In our example we will retrieve the data sorted on the insurance type. Here is the java example code:

 
package roseindia.tutorial.hibernate;
import org.hibernate.Session;
import org.hibernate.*;
import org.hibernate.cfg.*;
import java.util.*;
/**
 @author Deepak Kumar
 
 * http://www.roseindia.net HQL Order by Clause Example
 *  
 */
public class HQLOrderByExample {
  public static void main(String[] args) {
  Session session = null;
  try {
  // This step will read hibernate.
cfg.xml and prepare hibernate for
  // use
  SessionFactory sessionFactory =
 new Configuration().configure()
  .buildSessionFactory();
  session = sessionFactory.openSession();
  //Order By Example
  String SQL_QUERY = " from Insurance as 
insurance order by insurance.insuranceName";
  Query query = 
session.createQuery(SQL_QUERY);
  for (Iterator it 
= query.iterate(); it.hasNext();) {
  Insurance insurance = (Insurance) it.next();
  System.out.println("ID: " + insurance.
getLngInsuranceId());
  System.out.println("Name: " 
insurance.getInsuranceName());
  }
  session.close();
  catch (Exception e) {
  System.out.println(e.getMessage());
  finally {
  }
  }
}
To run the example select Run-> Run As -> Java Application from the menu bar. Following out is displayed in the Eclipse console:
Hibernate: select insurance0_.ID as col_0_0_ from insurance insurance0_ order by insurance0_.insurance_name
ID: 1
Hibernate: select insurance0_.ID as ID0_, insurance0_.insurance_name as insurance2_2_0_, insurance0_.invested_amount as invested3_2_0_, insurance0_.investement_date as investem4_2_0_ from insurance insurance0_ where insurance0_.ID=?
Name: Car Insurance
ID: 4
Hibernate: select insurance0_.ID as ID0_, insurance0_.insurance_name as insurance2_2_0_, insurance0_.invested_amount as invested3_2_0_, insurance0_.investement_date as investem4_2_0_ from insurance insurance0_ where insurance0_.ID=?
Name: Car Insurance
ID: 5
Hibernate: select insurance0_.ID as ID0_, insurance0_.insurance_name as insurance2_2_0_, insurance0_.invested_amount as invested3_2_0_, insurance0_.investement_date as investem4_2_0_ from insurance insurance0_ where insurance0_.ID=?
Name: Dental Insurance
ID: 11
Hibernate: select insurance0_.ID as ID0_, insurance0_.insurance_name as insurance2_2_0_, insurance0_.invested_amount as invested3_2_0_, insurance0_.investement_date as investem4_2_0_ from insurance insurance0_ where insurance0_.ID=?
Name: Home Insurance
ID: 12
Hibernate: select insurance0_.ID as ID0_, insurance0_.insurance_name as insurance2_2_0_, insurance0_.invested_amount as invested3_2_0_, insurance0_.investement_date as investem4_2_0_ from insurance insurance0_ where insurance0_.ID=?
Name: Home Insurance
ID: 2
Hibernate: select insurance0_.ID as ID0_, insurance0_.insurance_name as insurance2_2_0_, insurance0_.invested_amount as invested3_2_0_, insurance0_.investement_date as investem4_2_0_ from insurance insurance0_ where insurance0_.ID=?
Name: Life Insurance
ID: 3
Hibernate: select insurance0_.ID as ID0_, insurance0_.insurance_name as insurance2_2_0_, insurance0_.invested_amount as invested3_2_0_, insurance0_.investement_date as investem4_2_0_ from insurance insurance0_ where insurance0_.ID=?
Name: Life Insurance
ID: 6
Hibernate: select insurance0_.ID as ID0_, insurance0_.insurance_name as insurance2_2_0_, insurance0_.invested_amount as invested3_2_0_, insurance0_.investement_date as investem4_2_0_ from insurance insurance0_ where insurance0_.ID=?
Name: Life Insurance
ID: 9
Hibernate: select insurance0_.ID as ID0_, insurance0_.insurance_name as insurance2_2_0_, insurance0_.invested_amount as invested3_2_0_, insurance0_.investement_date as investem4_2_0_ from insurance insurance0_ where insurance0_.ID=?
Name: Medical Insurance
ID: 10
Hibernate: select insurance0_.ID as ID0_, insurance0_.insurance_name as insurance2_2_0_, insurance0_.invested_amount as invested3_2_0_, insurance0_.investement_date as investem4_2_0_ from insurance insurance0_ where insurance0_.ID=?
Name: Medical Insurance
ID: 13
Hibernate: select insurance0_.ID as ID0_, insurance0_.insurance_name as insurance2_2_0_, insurance0_.invested_amount as invested3_2_0_, insurance0_.investement_date as investem4_2_0_ from insurance insurance0_ where insurance0_.ID=?
Name: Motorcycle Insurance
ID: 14
Hibernate: select insurance0_.ID as ID0_, insurance0_.insurance_name as insurance2_2_0_, insurance0_.invested_amount as invested3_2_0_, insurance0_.investement_date as investem4_2_0_ from insurance insurance0_ where insurance0_.ID=?
Name: Motorcycle Insurance
ID: 7
Hibernate: select insurance0_.ID as ID0_, insurance0_.insurance_name as insurance2_2_0_, insurance0_.invested_amount as invested3_2_0_, insurance0_.investement_date as investem4_2_0_ from insurance insurance0_ where insurance0_.ID=?
Name: Travel Insurance
ID: 8
Hibernate: select insurance0_.ID as ID0_, insurance0_.insurance_name as insurance2_2_0_, insurance0_.invested_amount as invested3_2_0_, insurance0_.investement_date as investem4_2_0_ from insurance insurance0_ where insurance0_.ID=?
Name: Travel Insurance

Comments

Popular posts from this blog

Servlets, JSP, Struts and MVC

Hibernate Avg() Function (Aggregate Functions)

Hibernate Count Query