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

前端 未结 6 1543
名媛妹妹
名媛妹妹 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:39

    Both solutions offered here by Ruben and Aaron still don't "fully" work for pagination because the returned object (from executeQuery() and listDistinct) is an ArrayList (with up to max objects in it), and not PagedResultList with the totalCount property populated as I would expect for "fully" support pagination.

    Let's say the example is a little more complicated in that : a. assume Role has an additional rolename attribute AND b. we only want to return distinct User objects with Role.rolename containing a string "a" (keeping in mind that a User might have multiple Roles with rolename containing a string "a")

    To get this done with 2 queries I would have to do something like this :

    // First get the *unique* ids of Users (as list returns duplicates by
    // default) matching the Role.rolename containing a string "a" criteria
    def idList = User.createCriteria().list {
      roles {
        ilike( "rolename", "%a%" )
      }
      projections {
        distinct ( "id" )
      }
    }
    
    if( idList ){
      // Then get the PagedResultList for all of those unique ids
      PagedResultList resultList =
        User.createCriteria().list( offset:"5", max:"5" ){
          or {
             idList.each {
               idEq( it )
             }
          }     
          order ("username", "asc")
        }
    }
    

    This seems grossly inefficient.

    Question : is there a way to accomplish both of the above with one GORM/HQL statement ?

提交回复
热议问题