GORM createCriteria and list do not return the same results : what can I do?

前端 未结 6 1585
名媛妹妹
名媛妹妹 2020-12-16 23:51

I am using Nimble and Shiro for my security frameworks and I\'ve just come accross a GORM bug. Indeed :

User.createCriteria().list { 
   maxResults 10 
} 
         


        
6条回答
  •  生来不讨喜
    2020-12-17 00:31

    EDIT: Found a way to get both! Totally going to use it now

    http://www.intelligrape.com/blog/tag/pagedresultlist/

    If you call createCriteria().list() like this
    def result=SampleDomain.createCriteria().list(max:params.max, offset:params.offset){
    // multiple/complex restrictions
       maxResults(params.max)
       firstResult(params.offset)
    } // Return type is PagedResultList
    println result
    println result.totalCount
    

    You will have all the information you need in a nice PagedResultList format!

    /EDIT

    Unfortunately I do not know how to get a combination of full results AND max/offset pagination subset in the same call. (Anyone who can enlighten on that?)

    I can, however, speak to one way I've used with success to get pagination working in general in grails.

    def numResults = YourDomain.withCriteria() {
        like(searchField, searchValue)
        order(sort, order)
        projections {
          rowCount()
        }
    }
    
    def resultList = YourDomain.withCriteria() {
        like(searchField, searchValue)
        order(sort, order)
        maxResults max as int
        firstResult offset as int
    }
    

    That's an example of something I'm using to get pagination up and running. As KoK said above, I'm still at a loss for a single atomic statement that gives both results. I realize that my answer is more or less the same as KoK now, sorry, but I think it's worth pointing out that rowCount() in projections is slightly more clear to read, and I don't have comment privileges yet :/

    Lastly: This is the holy grail (no pun intended) of grails hibernate criteria usage references; bookmark it ;) http://www.grails.org/doc/1.3.x/ref/Domain%20Classes/createCriteria.html

提交回复
热议问题