findAll()HQL不在grails中返回分页列表 [英] findAll() HQL not returning paginated list in grails

查看:74
本文介绍了findAll()HQL不在grails中返回分页列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个grails应用程序,在这里我必须在list.gsp上应用过滤器框。当我使用以下查询进行过滤时(在我的服务中),我得到了分页列表

  def clientCriteria = TripOrder.createCriteria()
def searchResults = clientCriteria.list(max:params.max,offset:params.offset,sort:params.sort,order:params.order){
ilike( (搜索结果:searchResults,searchResultSize:searchResults.getTotalCount()]

但是我的问题是,当我使用findAll时,我无法获取分页列表,查询如下所示:

  def searchResults = TripOrder.findAll(TripOrder as t其中t.status.status =:status, [status:searchFor],[max:maximum,sort:params.sort,order:params.order])

println searchResults.size()

[searchResults:searchResults ,searchResultSiz e:searchResults.size()]

注意:由于某些原因我必须使用findAll()HQL代替条件查询。



以上结果仅提供等于max的列表数目,而不是提供分页列表



请提供我使用findAll()获取分页列表的解决方案。



谢谢。

解决方案

根据您的评论,您可以像这样做,只要您得到 PagedResultList

  def results = TripOrder.createCriteria.list(params){
customer {
ilike'firstName', %$ searchFor%
}
}

assert results.size()!= results.totalCount
findAll 或类似于findAll而不是条件,那么它基本上会触发另一个对totalCount的查询。你可以通过使用<$ c $来吸取更好的选择c> DetachedCriteria / 其中query ,它按需延迟执行查询。同样,您将无法在首次查询中获得总计数。

  def query = TripOrder.where {
customer.firstName =〜searchFor
}

//只有在调用list()时才执行查询
def results = query.list(params)

//只有当count()被称为
def totalCount = query.count()


I am working on a grails application, in this I have to apply filter box on list.gsp. When I am filtering using following query(in my service) I am getting paginated list :

    def clientCriteria = TripOrder.createCriteria()
        def searchResults = clientCriteria.list(max: params.max, offset: params.offset, sort: params.sort, order: params.order){
            ilike("origin", "${searchFor}%")
        }
        println searchResults.getTotalCount()
        [searchResults: searchResults, searchResultSize: searchResults.getTotalCount()]

But my problem is that when I am using findAll, I am not able to get paginated list, query as follows :

def searchResults =  TripOrder.findAll("from TripOrder as t where t.status.status=:status", [status: searchFor], [max: maximum, sort: params.sort, order: params.order])

            println searchResults.size()

            [searchResults: searchResults, searchResultSize: searchResults.size()]

Note : Because of some reasons I have to use findAll() HQL instead criteria queries.

Above result provide only number of list equal to max instead of provide paginated list.

Please provide me solution for getting paginated list using findAll().

Thanks.

解决方案

Based on your comment, you can do like this where you get a PagedResultList

def results = TripOrder.createCriteria.list(params) {
    customer {
       ilike 'firstName', "%$searchFor%" 
    }
}

assert results.size() != results.totalCount

It basically fires another query for the totalCount, if you want to stick to findAll or something like findAll instead of criteria then you can imbibe a better alternative by using a DetachedCriteria/where query which lazily executes the query on demand. Again, you won't be able to get the total count in first query. You have to fire another for the same.

def query = TripOrder.where {
    customer.firstName =~ searchFor
}

//Query executed only when list() is called
def results = query.list( params )

//Only executed when count() is called
def totalCount = query.count()

这篇关于findAll()HQL不在grails中返回分页列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆