Frameworks| Hibernate| Struts| JSF| JavaFX| Ajax| Spring| DOJO| JDO| iBatis| Questions?
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
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.
 
Search Tutorials:
 
Software Solutions and Services
 

 
Google Custom Search:
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation

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 = (Insuranceit.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

                         

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

5 comments so far (
post your own) View All Comments Latest 10 Comments:

is it possible to order on a property of an entity that is related to the main entity with a many-to-one property ?

Class A have a Class B property
Class B have a name or category property

is it possible to order by the name or the category ?

how ?
with A.b.name ... it is not working ?
I have a "org.hibernate.QueryException: could not resolve property:..."

Thanks,
Antoine

Posted by Antoine_h on Tuesday, 10.14.08 @ 04:11am | #81061

i want step by step creation of struts with hibernate example program

Posted by usha on Tuesday, 05.13.08 @ 15:56pm | #59713

hi,
I have solved the previous error but know it will show an other error
like

Hibernate 2.1.7
- hibernate.properties not found
- using CGLIB reflection optimizer
- using JDK 1.4 java.sql.Timestamp handling
- configuring from resource: /hibernate.cfg.xml
- Configuration resource: /hibernate.cfg.xml
- /hibernate.cfg.xml not found
/hibernate.cfg.xml not found

pls tell mee as soon as possible,

thanks
ravi

Posted by ravi on Wednesday, 02.13.08 @ 16:26pm | #48145

Hi
plz give some examples on may-to-many,many-to-one association.

Posted by Janmejay on Friday, 03.30.07 @ 10:32am | #12967

for (Iterator it = query.iterate(); it.hasNext();) {
Insurance insurance = (Insurance) it.next();
System.out.println("ID: " + insurance.getLngInsuranceId());
System.out.println("Name: " + insurance.getInsuranceName());

for This code we are getting insurance instance.coming to previous lession it is an object array type.May i know what is exact return type.

Thanks,
Bharathi

Posted by Bharathi on Tuesday, 03.27.07 @ 16:51pm | #12790

Training Courses
Tell A Friend
Your Friend Name
Search Tutorials:

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.