Home Hibernate Hibernate-how-to hibernateTemplate-Order Results



hibernateTemplate-Order Results
Posted on: January 19, 2008 at 12:00 AM
In this section you will learn how to add ordering ability to in Hibernate Application when using hibernateTemplate.

hibernateTemplate-Order Results

     

In this section you will learn how to add ordering ability to in Hibernate Application when using hibernateTemplate. There are many ways you can order your results when using hibernateTemplate.

Suppose you have a class called "Student" with the fields "studentName", "StudentCode" and you want to retrieve all the records from database in sorted by "studentName" in ascending order. There are following options for you.

  1. Use getHibernateTemplate().find() method
    Following code shows how you can use getHibernateTemplate().find() method:
    public Collection loadAllStudent() throws DataAccessException{
     
    return getHibernateTemplate().find("from Student student order by student.studentName");
    }
     
  2. Use getHibernateTemplate().findByCriteria()
    Following code shows how you can use getHibernateTemplate().findByCriteria() function in your hibernate application.
    public Collection loadAllStudent() throws DataAccessException{
       return getHibernateTemplate().findByCriteria(DetachedCriteria.forClass(
    Student.class).addOrder(Order.asc("studentName")));
    }
     

Related Tags for hibernateTemplate-Order Results:
cdatabaseexceptionhibernateclassantdatatemplateiosortloadmethodfindgetordercollectiondalfieldnamereturnusingfieldstabrowstudentshowpublicforoptionsrowsiecallrecordwithadanatrecordsresultstobaseoptionpossseldwsshethrowitrtelscanliusetrieulresultmanfromceindataaccesscalasmntsortedtrwantemposcaaacosadclesretrieveemendaseallmehowhrppwhensurnsusoesscolatanylatehaltsllcollectmplfollowexceptandascendingarcodcodecccollecttcordsrtwantwingscessrirdthshoavstaccessabendingfinldsmanyplndodeonomolo


More Tutorials from this section

Ask Questions?    Discuss: hibernateTemplate-Order Results  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.