Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create criteria in groovy/grails for nested object?

I need help on creating hibernate criteria for nested object. For example :

class office{
    Integer id;
    OfficeDetails cmdData ;
}

class OfficeDetails {
    Integer id;
    Region region;

}

class Region {
    Integer id;
    Integer regionNum;
}

Now, from the service class ( officeService) I am trying to pull up all of the offices that matches a certain region as :

List<Office> findAllByRegion( Integer regionNumber){
    def criteria =  {  eq ( 'cmdData.region.regionNum', regionNumber ) }
    def allOfficesInTheRegion =  Office.findAll(criteria)

    return allOfficesInTheRegion
}

Always getting exception :"org.hibernate.QueryException: could not resolve property:" I need to find out right way to create criteria for this query.Can anyone help?

like image 956
VictorGram Avatar asked Oct 17 '12 15:10

VictorGram


1 Answers

See "querying associations" under the criteria section of the user guide:

def criteria = {
  cmdData {
    region {
      eq('regionNum', regionNumber)
    }
  }
}
like image 57
Ian Roberts Avatar answered Oct 13 '22 22:10

Ian Roberts