本文介绍了GORM createCriteria和list不会返回相同的结果:我该怎么办?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用和为我的安全框架,我刚刚穿过GORM bug。确实:

pre $ User.createCriteria().list {
maxResults 10
}

返回 10个用户,而 User.list(max:10)会返回 9个用户

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

看起来任何具有多于1个角色的用户都会在 createCriteria call和 User.list 将返回 max-1 实例(即9个用户而不是10个用户) p>

我可以使用什么解决方法才能返回10个唯一身份用户?

是一个非常恼人的,因为我没有办法正确使用分页。






我的网域类别为:

  class UserBase {
字符串用户名
静态belongsTo = [角色,组] $ b $静态hasMany = [角色:角色,组:组]
static fetchMode = [角色:'eager',groups:'eager']
static mapping = {
角色缓存:true,
cascade:'none',
缓存使用情况:'读写',包括:'all'
}
}

class User扩展UserBase {
static mapping = {cache:'read-write'}
}
$ b $ class $ {
static hasMany = [users:UserBase,groups:Group]
static belongsTo = [Group]
static mapping = {cache usage:'读写,包括:'all'
用户缓存:true
组缓存:true
}
}


解决方案

简洁明了,但使用HQL查询似乎是解决此问题的一种方法。如(executeQuery部分)中所述,分页参数可以作为额外参数添加执行查询。

  User.executeQuery(从用户用户中选择不同的用户,[max:2,offset:2]) 


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
}

returns 10 users whereas User.list(max: 10) returns 9 users !

After further investigations, I found out that createCriteria returns twice the same user (admin) because admin has 2 roles!!! (I am not joking).

It appears that any user with more than 1 role will be returned twice in the createCriteria call and User.list will return max-1 instances (i.e 9 users instead of 10 users)

What workaround can I use in order to have 10 unique users returned ?

This is a very annoying because I have no way to use pagination correctly.


My domain classes are:

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
  }
}
解决方案

Less concise and clear, but using an HQL query seems a way to solve this problem. As described in the Grails documentation (executeQuery section) the paginate parameters can be added as extra parameters to executeQuery.

User.executeQuery("select distinct user from User user", [max: 2, offset: 2])

这篇关于GORM createCriteria和list不会返回相同的结果:我该怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 13:57