Grails findAll with sort, order, max and offset?

前端 未结 4 1503
时光取名叫无心
时光取名叫无心 2021-02-19 18:25

I want to integrate sort, order, max and offset in a findAll query. The following works fine:

def books = Book.findAll(\"from Book as b where b.approved=true ord         


        
4条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-19 18:40

    HQL doesn't support sort and order as parameters, so you need to include the "order by" as part of the HQL expression

    def books = Book.findAll("from Book as b where b.approved=true"
      + " order by b.dateCreated desc", [max: max, offset: offset])
    

    (or in this case just use Book.findAllByApproved(true, [...]) instead of HQL).

    So if the sort and order are variables you need a trick like

    def books = Book.findAll("from Book as b where b.approved=true"
      + (params.sort ? " order by b.${params.sort} ${params.order}" : ''), 
      [max: max, offset: offset])
    

提交回复
热议问题