问题描述
我使用和为我的安全框架,我刚刚穿过GORM bug。确实:
pre $ User.createCriteria().list {
maxResults 10
}
返回 10个用户,而 User.list(max:10)
会返回 9个用户!
经过进一步调查后,我发现 createCriteria
返回两次相同的用户 admin)因为管理员有两个角色! (我不是在开玩笑)。
看起来任何具有多于1个角色的用户都会在 我可以使用什么解决方法才能返回10个唯一身份用户? 是一个非常恼人的,因为我没有办法正确使用分页。 我的网域类别为: 简洁明了,但使用HQL查询似乎是解决此问题的一种方法。如(executeQuery部分)中所述,分页参数可以作为额外参数添加执行查询。 I am using Nimble and Shiro for my security frameworks and I've just come accross a GORM bug. Indeed : returns 10 users whereas After further investigations, I found out that It appears that any user with more than 1 role will be returned twice in the 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: 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. 这篇关于GORM createCriteria和list不会返回相同的结果:我该怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! createCriteria $ c $中返回两次c> call和
User.list
将返回 max-1
实例(即9个用户而不是10个用户) p>
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
}
}
User.executeQuery(从用户用户中选择不同的用户,[max:2,offset:2])
User.createCriteria().list {
maxResults 10
}
User.list(max: 10)
returns 9 users ! createCriteria
returns twice the same user (admin) because admin has 2 roles!!! (I am not joking).createCriteria
call and User.list
will return max-1
instances (i.e 9 users instead of 10 users) 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
}
}
User.executeQuery("select distinct user from User user", [max: 2, offset: 2])