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

时间:2015-06-05 14:08:09

标签: grails gorm

我有三个域名classess:

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为父级,而父级则为Cafee。 我知道如何按父母搜索,例如:

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

但是如何通过祖父母来搜索?

1 个答案:

答案 0 :(得分:2)

使用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将是一种合理的方法,而不是动态查找器。