这些天我使用Grails3.3和GORM CriteriaBuilder进行大多数查询,但是我被困在如何调用属性上的函数上-例如,在日期属性上调用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中有什么方法可以做到这一点吗?我知道我可以回到domain类并定义一个从date字段中提取小时的映射,但这看起来有点笨拙。
发布于 2018-05-31 06:38:38
试一试sqlRestriction
:
def results = Student.withCriteria {
sqlRestriction 'hour(registration) = 15'
}
请参阅http://gorm.grails.org/latest/hibernate/manual/index.html#criteria中的7.5.6. Using SQL Restrictions
https://stackoverflow.com/questions/50613830
复制相似问题