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

耗尽温柔 提交于 2019-12-04 17:56:45

问题


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?


回答1:


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

def criteria = {
  cmdData {
    region {
      eq('regionNum', regionNumber)
    }
  }
}


来源:https://stackoverflow.com/questions/12938219/how-to-create-criteria-in-groovy-grails-for-nested-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!