我正在使用 Nimble和 Shiro对于我的安全框架,我刚刚遇到了一个 GORM 错误。的确 :
User.createCriteria().list {
maxResults 10
}
返回 10 个用户 而
User.list(max: 10)
返回 9 位用户 !经过进一步调查,我发现
createCriteria
返回两次相同的用户 (管理员) 因为管理员有 2 个角色 !!! (我不是在开玩笑)。似乎任何具有超过 1 个角色的用户都会在
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
}
}
最佳答案
不太简洁明了,但使用 HQL 查询似乎是解决此问题的一种方法。如 Grails documentation 中所述(executeQuery 部分)分页参数可以作为额外的参数添加到 executeQuery。
User.executeQuery("select distinct user from User user", [max: 2, offset: 2])
关于grails - GORM createCriteria 和 list 不返回相同的结果 : what can I do?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3905476/