Grails不在另一组结果中查找(或在Spring中查找非管理员用户)的标准

Grails不在另一组结果中查找(或在Spring中查找非管理员用户)的标准,grails,spring-security,gorm,Grails,Spring Security,Gorm,域名支出: class User { String username static hasMany = [userRoles: UserRole] } class Role { String authority } class UserRole { User user Role role static belongsTo = [User, Role] } 这是安装GrailsSpringSecurity核心插件时通常会看到的压缩版本 现在,一个用

域名支出:

class User {
    String username
    static hasMany = [userRoles: UserRole]
}
class Role {
    String authority
}
class UserRole {
    User user
    Role role
    static belongsTo = [User, Role]
}
这是安装GrailsSpringSecurity核心插件时通常会看到的压缩版本

现在,一个用户可以有多个角色。因此,管理员可以是普通用户。用户:UserRole=1:M

在SQL中,很容易获得一组没有附加用户角色的用户。 如何使用Grails criteria builder实现这一点

作为一个权宜之计(直到我得到一个很好的答案,基本上不是将SQL插入Grails),我基本上将所有管理员用户作为一个集合,然后将用户列表作为另一个集合,然后逐个删除所有管理员用户。难看,但它工作(缓慢):

在上面的代码中,如果您执行f.removeAll(现有),它将不起作用

我认为这将是一个很好的问题,因为您也可以将其应用于其他场景


提前谢谢

我不确定如何使用条件查询,但这个HQL可以工作:

def role = ...
def usersWithoutRole = UserRole.executeQuery(
   'from User u where u not in ' +
       '(select ur.user from UserRole ur where ur.role=:r)',
   [r: role])
def role = ...
def usersWithoutRole = UserRole.executeQuery(
   'from User u where u not in ' +
       '(select ur.user from UserRole ur where ur.role=:r)',
   [r: role])