To sort your HQL query's results, you will need to use the ORDER BY clause. You can order the results by any property on the objects in the result set either ascending (ASC) or descending (DESC). Following is the simple syntax of using ORDER BY clause:
String hql ="FROM Employee E WHERE E.id > 10 ORDER BY E.salary DESC";
Query query =session.createQuery(hql); List results =query.list(); If you wanted to sort by more than one property, you would just add the additional properties to the end of the order by clause, separated by commas as follows:
String hql ="FROM Employee E WHERE E.id > 10 "+
"ORDER BY E.firstName DESC, E.salary DESC "; Query query =session.createQuery(hql); List results =query.list(); |