我有一个用户域和一个角色域以及一个在用户端编码为

static hasMany = [ roles: Role ]
...
static mapping = {
    table 'user_data'
    id column: 'employee_number', name: 'employeeNumber', generator: 'assigned', type: 'int'
    version false
    sort 'lastName'

    roles joinTable: [ name: 'user_role' ]

}

我正在尝试查询数据库以拉动所有具有安全员角色的用户
def roleInstance = Role.find { name == 'security_officer' }
def secList = User.findAll("from User as u where u.roles = :roleInstance", [roleInstance:roleInstance])

但是我得到了错误
Class: com.microsoft.sqlserver.jdbc.SQLServerException
Message: The value is not set for the parameter number 1.

我究竟做错了什么?

最佳答案

我通过一堆猜测和检查来弄清楚。

 def roleInstance = Role.findByName("security_officer")
 def query = User.where {
     roles {
         id == roleInstance.id
     }
 }
 def securityOfficerList = query.list()

09-26 11:01