我有三个域类:
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作为父级,而Cafes作为父级。
我知道如何按 parent 搜索,例如:
def table = TablePlacesInfo.findWhere(hall : params['hallsAvailable'], placesInTableAmount : Integer.parseInt(params['tablePlacesAvailable']))
但是,如何通过祖 parent 进行搜索呢?
最佳答案
使用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"""
)
选择
DetachedCriteria
或where
将是一种合理的方法,而不是动态查找器。
关于grails - 通过Grails中的动态查找器进行请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30668647/