Grails joinTable查询问题
Grails joinTable query issue
我有一个用户域和一个角色域以及一个在用户端编码为
的工作 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.
我做错了什么?
角色是一种 hasMany 关系,所以我认为以下应该有效。
def secList = User.findAll("from User as u where u.roles in (:roleInstance)", [roleInstance:[roleInstance]])
用户有很多角色,所以在查询中你不能使用u.roles = roleInstance.Try在[角色列表]中使用或者你可以尝试以下查询:
def secList = User.findAll("from User as u where u.roles in (from Role r where r.name=:roleInstance)", [roleInstance:roleInstance])
我通过一堆猜测和检查弄明白了。
def roleInstance = Role.findByName("security_officer")
def query = User.where {
roles {
id == roleInstance.id
}
}
def securityOfficerList = query.list()
我有一个用户域和一个角色域以及一个在用户端编码为
的工作 joinTablestatic 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.
我做错了什么?
角色是一种 hasMany 关系,所以我认为以下应该有效。
def secList = User.findAll("from User as u where u.roles in (:roleInstance)", [roleInstance:[roleInstance]])
用户有很多角色,所以在查询中你不能使用u.roles = roleInstance.Try在[角色列表]中使用或者你可以尝试以下查询: def secList = User.findAll("from User as u where u.roles in (from Role r where r.name=:roleInstance)", [roleInstance:roleInstance])
我通过一堆猜测和检查弄明白了。
def roleInstance = Role.findByName("security_officer")
def query = User.where {
roles {
id == roleInstance.id
}
}
def securityOfficerList = query.list()