【问题标题】:Request via dynamic finders in Grails通过 Grails 中的动态查找器请求
【发布时间】:2015-08-20 12:41:48
【问题描述】:

我有三个领域类:

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

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

但如何也按祖父母搜索?

【问题讨论】:

    标签: grails grails-orm


    【解决方案1】:

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

    【讨论】:

    • id 在域类中默认为 Long,因此 1L (Long 1) 用于比较,尽管在 where 查询的情况下它会被强制,但我是具体的。
    猜你喜欢
    • 2011-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多