grails在createcriteria上使用order

时间:2013-07-13 19:14:09

标签: grails pagination createcriteria

我尝试使用CreateCriteria来获取一些ID。由于ListDistinct不支持分页, 我在网上找到了解决此问题的解决方案。 http://ondrej-kvasnovsky.blogspot.fr/2012/01/grails-listdistinct-and-pagination.html

但是当我尝试使用sort和order获取元素时,我有一个例外:

“按表达式排序”THIS_.DATE“必须在此案例的结果列表中; SQL语句:...”

我的代码:

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
    }

  } 
}

1 个答案:

答案 0 :(得分:1)

探测可能是在你的结果中没有日期列。 我没有测试过,但在查看这个问题之后:What is the sql query for this?(对第一个答案的评论)我认为添加

property("date")

你的预测可以提供帮助。

---------------- EDIT ----------------------------

这是针对您的问题的完整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
        }

    } 
}
相关问题