Gorm:按 属性 相关字段过滤条件

Gorm: Filtering criteria by a property related field

我有以下条件:

Alert.createCriteria().add(Restrictions.isNull('resolvedDate'))

我需要使用 "property's property" 字段进行过滤:alert.device.room.id 类似于:

Restriction.eq("alert.device.room.id", roomId)

如何将此限制添加到主要条件?

您可以使用 HibernateCriteriaBuilder 的 DSL 在更多的 Grails 'way' 中编写它:

使用嵌套闭包

Alert.createCriteria().list() {
    isNull('resolvedDate')

    device {
        room {
            idEq(roomId)
        }
    }
}

idEq(roomId) 可以替换为 eq('id', roomId),然后也可以与 non-id 属性一起使用。

使用 createAlias

Alert.createCriteria().list() {
    isNull('resolvedDate')

    createAlias('device', 'd')
    createAlias('d.room', 'r')
    eq('r.id', roomId)
}