grails - 通过Grails中的动态查找器进行请求

标签 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 : 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"""
)

选择DetachedCriteriawhere将是一种合理的方法,而不是动态查找器。

关于grails - 通过Grails中的动态查找器进行请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30668647/

相关文章:

Grails glink 可选参数属性未根据自定义 urlMappings 过滤

grails - 如何使GORM在Map上创建索引?

grails - 映射破坏了我的域名类(class)

hibernate - 尽管没有关联,但调用.list()时,SQL每行运行一次

grails - 在Grails应用程序 Controller 外部使用了类[]上的方法

grails cxf-client 插件 wsdlArgs -exsh 意外选项 :

grails - 您没有权限… Spring 安全四郎

spring - 如何在 Grails 中不断检查上传大小?

grails - GORM notIn带有别名的子查询

grails - Grails控件联接表