这是mycode:

       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();

关于jakarta-ee - 如何下单子(monad)句条件查询,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29301154/

10-09 19:15