0

我有以下内容:

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)

如何仅返回具有特定角色的用户?

谢谢

4

1 回答 1

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
于 2012-11-06T13:11:59.347 回答