Home Hibernate Hibernate-how-to hibernateTemplate-Order Results
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

hibernateTemplate-Order Results

Advertisement
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")));
    }
     
Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: January 19, 2008

Ask Questions?    Discuss: hibernateTemplate-Order Results  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com