Search code examples
grailswhere-clause

find list using createcriteria nested object property


I have a domain like this

 Employee {
   Address address
   String name
   String title
}

and another domain

 Address {
  String country
  String locality 
  String city
}

and now i want to find the all the employee with given city something like that i tried

  def employees = Employee.where { 
      address {
       city == params.city
        }
   }

but this is not working , how can i achieve this


Solution

  • You can write it like following

    List<Employee> employees=Employee.createCriteria().list{
        'address'{
             eq('city',params.city)
         }
    
    }
    

    Note: When the eq('city',city) (same parameter name) is used, it will not work.