HQL语句--order by(排序)

order by:对查询结果排序

  1. 升序排序 asc (默认升序)
  2. 降序排序 desc
@Test
    public void testOrderby(){
        String hql="from Commodity order by price asc"; //升序
        Query query=session.createQuery(hql);
        List commodities=query.list();

        for (Commodity commodity : commodities) {
            System.out.println("name:"+commodity.getName());
            System.out.println("price:"+commodity.getPrice());
        }       
    }

HQL语句--order by(排序)_第1张图片

设置多个排序规则:

@Test
    public void testOrderby(){
        //先按商家id升序 ;若id一致,按价格降序;若价格一致,按名称升序
        String hql="from Commodity order by seller.id asc,price desc,name asc";  
        Query query=session.createQuery(hql);
        List commodities=query.list();

        for (Commodity commodity : commodities) {
            System.out.println("name:"+commodity.getName());
            System.err.println("sellerID:"+commodity.getSeller().getId()); //商家id
            System.err.println("sellerName"+commodity.getSeller().getName()); //商家名称
            System.out.println("price:"+commodity.getPrice());
        }       
    }

部分:
HQL语句--order by(排序)_第2张图片

你可能感兴趣的:(hql,Hibernate)