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

Request via dynamic finders in Grails

我有三个域类:

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
    }
}

如您所见,类之间通过链相互连接:

Cafee-(hasMany)->HallsZones-(hasMany)->TablePlacesInfo.

我想获取 TablePlaces 信息,其中 HallsZones 为 parent,而 Cafee 为 parent。 我知道如何按 parent 搜索,例如:

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

但是怎么也用grandparent搜索呢?

使用where查询:

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

使用Criteria:

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"""
)

选择 DetachedCriteriawhere 将是替代动态查找器的合理方法。