Grails - 查找日期范围重叠的位置

时间:2013-05-02 14:14:12

标签: grails gorm

我有一个带有startDate和endDate属性的Grails域对象。

找到范围[startDate,endDate]与指定日期范围重叠的所有对象的最佳方法是什么?我知道如何在SQL中执行此操作,但想知道是否有任何Grails / GORM魔术可以更简洁地完成。

此外,endDate是一个可选属性。

SQL / JPQL查询类似于

from MyObject obj where obj.startDate <= ?1 and (obj.endDate is null OR obj.endDate >= ?2)

1 个答案:

答案 0 :(得分:7)

在这种情况下接受Grails / GORM的两种方法:

<强>懒惰: -

def today = new Date()
def query = MyObject.where {
    startDate <= (today - 10) && (endDate == null || endDate >= today + 10)
}
def listOfMyObjects = query.list()

<强>乐意: -

def today = new Date()
def listOfMyObjects = MyObject.findAll {//or find{} if you need the first occurance
    startDate <= (today - 10) && (endDate == null || endDate >= today + 10)
}
相关问题