0

这些天来,我在大部分查询中都使用 Grails 3.3 和 GORM CriteriaBuilder,但我被困在如何调用属性上的函数——例如,调用 Date 属性上的 hour() 函数。如果我使用 where DetachedCriteria 我可以有这样的东西:

def whereQuery = Student.where {
    hour(registration) = 15
}

查找在 15:00 到 15:59 之间注册的所有学生。

但是,如果我改用 CriteriaBuilder,我不能这样做:

def c = Student.createCriteria()
def results =  c.list {
    eq 'hour(registration)', 15
}

有没有办法在构建器 DSL 中实现这一点?我知道我可以回到域类并定义一个从日期字段中提取小时的映射,但这似乎有点笨拙。

4

1 回答 1

1

试一试sqlRestriction

def results = Student.withCriteria {
    sqlRestriction 'hour(registration) = 15'
}

参阅7.5.6. Using SQL Restrictionshttp://gorm.grails.org/latest/hibernate/manual/index.html#criteria

于 2018-05-30T22:38:38.467 回答