Search code examples
grailsgrails-orm

How to get access to properties of related table via WHERE clause?


For example I have follow domains:

class Company {
    String name 
}

class Employee {
    String firstName
    String secondName
    Company company
}

And I try to get all employees where company name is 'M$oft'

Employee.where {
   eq 'company.name', 'M$oft'
}.list()

Got:

could not resolve property: company.name of: myapp.Employee


Also I try this:

Employee.where {
   createAlias('company', 'c')
   eq 'c.name', 'M$oft'
}.list()

Got:

could not resolve property: c of: myapp.Employee


I know about:

Employee.where {
    company.name == 'M$oft'
}

but it's not appropriate for me.

I just want to know: is it possible do it via where clause with usage of string names of fields?


Solution

  • Use next syntax:

     Employee.where {
       company{
         eq 'name', 'M$oft'
       }
    }.list()
    

    And don't forget to use ' ' with strings that have $. Maybe some unexpected result.