代码之家  ›  专栏  ›  技术社区  ›  fabien7474 Jan Tchärmän

gorm createCriteria和list不返回相同的结果:我能做什么?

  •  7
  • fabien7474 Jan Tchärmän  · 技术社区  · 15 年前

    我正在使用 Nimble Shiro 对于我的安全框架,我刚刚遇到了一个Gorm bug。的确:

    User.createCriteria().list { 
       maxResults 10 
    } 
    

    收益率 10用户 反之 User.list(max: 10) 收益率 9用户 !

    经过进一步的调查,我发现 createCriteria 返回同一用户的两倍 (行政) 因为管理员有两个角色 !!!!(我不是开玩笑)。

    创造准则 调用和 User.list 将返回 max-1 实例(即9个用户而不是10个用户)

    我可以使用什么解决方案来返回10个唯一用户?

    这很烦人,因为我没有正确使用分页的方法。


    我的域类是:

    class UserBase { 
       String username 
       static belongsTo = [Role, Group] 
       static hasMany = [roles: Role, groups: Group] 
       static fetchMode = [roles: 'eager', groups: 'eager'] 
       static mapping = { 
         roles cache: true, 
         cascade: 'none', 
         cache usage: 'read-write', include: 'all' 
       } 
    }
    
    class User extends UserBase { 
      static mapping = {cache: 'read-write'} 
    } 
    
    class Role { 
      static hasMany = [users: UserBase, groups: Group] 
      static belongsTo = [Group] 
      static mapping = { cache usage: 'read-write', include: 'all' 
        users cache: true 
        groups cache: true 
      } 
    } 
    
    6 回复  |  直到 13 年前
        1
  •  4
  •   Ruben    14 年前

    Grails documentation

    User.executeQuery("select distinct user from User user", [max: 2, offset: 2])
    
        2
  •  3
  •   Aaron Saunders    14 年前

    User.createCriteria().listDistinct {
        maxResults(params.max as int)
        firstResult(params.offset as int)
        order(params.order, "asc")
    }
    
        3
  •  2
  •   Will Buck    13 年前

    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
    

    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
    }
    

    http://www.grails.org/doc/1.3.x/ref/Domain%20Classes/createCriteria.html

        4
  •  1
  •   Koka    14 年前

    // 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")
        }
    }
    

        5
  •  0
  •   Ruben    15 年前

    User.createCriteria().listDistinct {
        maxResults 10
    }
    
    推荐文章