0

I have a User domain and a Role domain and a working joinTable coded on the User side as

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' ]

}

I am trying to query the database to pull all users with a security officer role with

def roleInstance = Role.find { name == 'security_officer' }
def secList = User.findAll("from User as u where u.roles = :roleInstance", [roleInstance:roleInstance]) 

But I am getting the error

Class: com.microsoft.sqlserver.jdbc.SQLServerException
Message: The value is not set for the parameter number 1.

What am I doing wrong?

mjswartz
  • 715
  • 1
  • 6
  • 19

3 Answers3

1

I figured it out with a bunch of guess and checking.

 def roleInstance = Role.findByName("security_officer")
 def query = User.where { 
     roles { 
         id == roleInstance.id 
     } 
 }
 def securityOfficerList = query.list()
mjswartz
  • 715
  • 1
  • 6
  • 19
0

Roles is a hasMany relationship so I think following should work.

def secList = User.findAll("from User as u where u.roles in (:roleInstance)", [roleInstance:[roleInstance]]) 
Uday
  • 619
  • 3
  • 7
0

User has many roles, so in query you can't use u.roles = roleInstance.Try to use in [list of roles] or you can try the following query: def secList = User.findAll("from User as u where u.roles in (from Role r where r.name=:roleInstance)", [roleInstance:roleInstance])