如何按条款条件查询下订单

How to place order by clause criteria query

这是我的代码:

       public List<Veiculo> getAll()
   {

      CriteriaQuery<Veiculo> criteria = this.entityManager
            .getCriteriaBuilder().createQuery(Veiculo.class);
      return this.entityManager.createQuery(  
            criteria.select(criteria.from(Veiculo.class))).getResultList();
   }

现在,我需要一个条件查询的 order by 子句。

orderby veiculo.getServico

我试过了,但是不行

   public List<Veiculo> getAll()
   {

      CriteriaQuery<Veiculo> criteria = this.entityManager
            .getCriteriaBuilder().createQuery(Veiculo.class);
      return this.entityManager.createQuery(  
            criteria.select(criteria.from(Veiculo.class)).orderBy(getVeiculo().getServico())).getResultList();
   }

以下重写应该适合你,没有编译所以考虑作为一个示例元代码

    CriteriaBuilder cb = this.entityManager
                .getCriteriaBuilder();
    CriteriaQuery<Veiculo> criteria = cb.createQuery(Veiculo.class);
    Root<Veiculo> root = criteria.from(Veiculo.class);
    return this.entityManager.createQuery(
                 criteria.select(root).orderBy(cb.asc(root.get("servico"))))
           .getResultList();