0

I try to use CreateCriteria to fetch some ids. Since ListDistinct not support pagination, I found a solution in the web to resolve this issue. http://ondrej-kvasnovsky.blogspot.fr/2012/01/grails-listdistinct-and-pagination.html

But I when I tried to fetch elements with sort and order I had an exception:

"Order by expression "THIS_.DATE" must be in the result list in this case; SQL statement:..."

My code:

class MyClassA {

Date date
static hasMany =  [userList:User]

}


class User {

   String login

}



class ResponseService {

  def load(offset, max ) {

    def idList =  MyClassA.createCriteria().list (max: max, offset: offset) {
      projections { distinct ( "id" ) }
      userList{ 
      eq("login","toto") 

  }
      order("date","desc")
   }

  if( idList ) { 

  // fetch all Responses based on selected IDs
   def results =  MyClassA.getAll( idList )
   return results
    }

  } 
}
4

1 回答 1

3

问题可能是您的结果中没有日期列。我还没有测试过,但是在看了这个问题之后:什么是这个的 sql 查询?(评论第一个答案)我认为添加

property("date")

对您的预测有帮助。

- - - - - - - - 编辑 - - - - - - - - - - - - - -

这是针对您的问题的完整 ResponseService 类。我还添加property("id")了您的预测条款。

class ResponseService {

    def load(offset, max ) {

        def idList =  MyClassA.createCriteria().list (max: max, offset: offset) {
            projections { 
                distinct ( "id" ) 
                property("date")
                property("id")
            }
            userList{ 
                eq("login","toto") 
            }
            order("date","desc")
        }

        if( idList ) { 

            // fetch all Responses based on selected IDs
            def results =  MyClassA.getAll( idList )
            return results
        }

    } 
}
于 2013-07-13T19:37:57.217 回答