我有以下内容:
def userInstance=User.findAll("from User u where u.merchant is not null and u.merchant.id = ? and u.authorities.authority.contains('ROLE_MERCHANT')", merchantId)
并收到以下错误:
org.hibernate.hql.ast.QuerySyntaxException: unexpected AST node: ( near line 1, column 121 [from com.testing.tests.User u where u.merchant is not null and u.merchant.id = ? and u.authorities.authority.contains('ROLE_MERCHANT')]
at $Proxy20.createQuery(Unknown Source)
at testing.admin.UserService.findByMerchantId(UserService.groovy:14)
如何只返回具有特定角色的用户?
由于
答案 0 :(得分:5)
如果您查看User#getAuthorities
的默认实现,您会注意到该属性本身是使用动态查找程序调用实现的:
UserRole.findAllByUser(this).collect { it.role } as Set
因此,您无法在HSQL查询中引用authorities
,但您可以通过多种方式查询具有UserRole
关联域类特定角色的用户:
def users = UserRole.withCriteria {
role{
eq 'authority', 'ROLE_SOMETHING'
}
projections{
property 'user'
}
}
或
def users = UserRole.findAllByRole(domainRole)?.user