I am new to Spring3 and Hibernate the following code works great but I am trying to find a way to have my list returned in sort order by the date field. Can someone please show me how to add sort to this code
// To get list of all articles
@SuppressWarnings("unchecked")
public List<Friend> listFriends(String rUser)
{
Friend friend = new Friend();
friend.setUsername(rUser);
return (List<Friend>) sessionFactory.getCurrentSession()
.createCriteria(Friend.class)
.add(Example.create(friend))
.list();
}
Setting the Sorting Order. The Order class has two methods to set the sorting order: asc(String attribute) : Sorts the query by attribute in ascending order. desc(String attribute) : Sorts the query by attribute in descending order.
By default, rows are sorted in ascending order. For example, to sort rows from the ORDERS table by customer ID values, specify 1 for Level and A for Asc/Desc for the CUST_ID column, as shown in the following figure. Once you specify the sort criteria, press ENTER or use END.
In hibernate a sorted collection is sorted in memory being Java the responsible of sorting data using compareTo method. Obviously this method is not the best performance-way to sort a collection of elements.
.addOrder( Order.desc("date") )
Check the examples in the documentation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With