0

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

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.

我究竟做错了什么?

4

3 回答 3

1

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

 def roleInstance = Role.findByName("security_officer")
 def query = User.where { 
     roles { 
         id == roleInstance.id 
     } 
 }
 def securityOfficerList = query.list()
于 2015-10-16T17:30:21.380 回答
0

Roles 是一个 hasMany 关系,所以我认为以下应该有效。

def secList = User.findAll("from User as u where u.roles in (:roleInstance)", [roleInstance:[roleInstance]]) 
于 2015-10-16T16:46:57.040 回答
0

用户有很多角色,所以在查询中你不能使用 u.roles = roleInstance.Try to use in [list of roles] 或者你可以试试下面的查询: def secList = User.findAll("from User as u where u .roles in (from Role r where r.name=:roleInstance)", [roleInstance:roleInstance])

于 2015-10-16T16:52:34.323 回答