Here's an example of how I use the
HibernateCriteria API to order a query result based on a property of a Java class for which I am querying:
[url] User user = new User();
Session session = HibernateUtil.beginTransaction();
Criteria criteria = session.createCriteria(User.class);
Order order = Order.asc("loginName");
criteria.addOrder(order);
List results = criteria.list();
HibernateUtil.commitTransaction();
for (int i = 0; i<results.size(); i++) {
System.out.println(results.get(i).toString());
}
[/url]
Here's a little tutorial on using the Critieria API that you might find usefuL:
Quote:
A common requirement for any application is to have the results that are generated sorted by a particular column or attribute. Again, the Criteria API makes sorting your results extremely easy by providing you with an addOrder method in the
Critieriaclass.
The addOrder method takes something called an Order object as an argument, which itself is a pretty straight forward component.
An Order object is created by using one of the two static methods in the Order class, namely the static asc(String) and desc(String) methods. Basically, you just chose whether you want your list to be sorted in ascending order, or descending order, and pass to the appropriate method the name of the field on which you want to sort. So, to sort all of my User's based on their loginName, in a descending order, I would create an Order object like so:
Order order = Order.asc("loginName");
Then, you just pass the order object to the Criteria object's addOrder method, and your results will come back ordered in exactly the manner that you have specified.
Check out the JavaDoc for the Order:
http://www.hibernate.org/hib_docs/v3/api/org/hibernate/criterion/Order.htmlhttp://jpa.ezhibernate.com/Javacode/learn.jsp?tutorial=09howtousethecriteriaapi