简体   繁体   中英

How do I sort list with criteria in hibernate

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();
}
.addOrder( Order.desc("date") )

查看文档中示例

Either have the database do it or Java, your choice. Here's how to do it in Java:

List<Friend> friends = sessionFactory.getCurrentSession()
       .createCriteria(Friend.class)
        .add(Example.create(friend))
        .list();
Collections.sort(friends); 

return friends;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM