通过Grails中的动态查找器请求

通过Grails中的动态查找器请求,grails,gorm,Grails,Gorm,我有三个领域类别: class Cafee { String cafeeName static hasMany = [halls: HallsZones] static constraints = { halls nullable: true } } class HallsZones { String hallName static scaffold = true static hasMany = [table :

我有三个领域类别:

class Cafee {

    String cafeeName

    static hasMany = [halls: HallsZones]

    static constraints = {
        halls nullable: true
    }
}

class HallsZones {
    String hallName

    static scaffold = true
    static hasMany = [table : TablePlacesInfo]
    static belongsTo = [cafee : Cafee]

    static constraints = {
        table nullable: true
        cafee nullable: true
    }
}

class TablePlacesInfo {
    int placesInTableAmount
    int tableAmount
    int tableForReservationAmount
    int placeCost
    String currencyType

    static scaffold = true
    static belongsTo = [hall: HallsZones]

    static constraints = {
        hall nullable: true
    }
}
如您所见,classess通过链相互连接:

Cafee-(hasMany)->HallsZones-(hasMany)->TablePlacesInfo.
我想获得TablePlaces信息,它以HallsZones为家长,而HallsZones又以Cafee为家长。 我知道如何按家长搜索,例如:

def table = TablePlacesInfo.findWhere(hall : params['hallsAvailable'], placesInTableAmount : Integer.parseInt(params['tablePlacesAvailable'])) 

但是如何按祖父母搜索呢?

使用
where
查询:

TablePlacesInfo.where {
    hall {
        cafee {
            // criteria matching grand parent
            id == 1L // for example
        }
    }
}.list()
使用
标准

TablePlacesInfo.withCriteria {
    hall {
        cafee {
            // criteria matching grand parent
            idEq 1L // for example
        }
    }
}
使用hql:

TablePlacesInfo.executeQuery(
    """select tpi from TablePlacesInfo as tpi 
       inner join tpi.hall as hall 
       inner join hall.cafee as caf 
       where caf.id = 1"""
)

选择
DetachedCriteria
where
将是一种可靠的方法,而不是动态查找程序。

id
在域类中默认为长,因此1L(长1)用于比较,尽管在where查询的情况下,它将被强制为长,但我是特定的。