简体   繁体   中英

How to write conditions in grails createCriteria()

I have a domain class Feedback

class Feedback{
    String name
    String email
    Date createdOn
    Boolean isMailSent
}

I want to fetch all feedbacks with the condition as below 

1) isMailSent ==  false

2) (createdOn - cuurent time) >= 15 minutes 

How to write these conditions in createCriteria() methiod.

what i have tried here

def feedbackList = Feedback.createCriteria().list(
        ) {
            eq('isMailSent ',  false)
            /** here condition (createdOn - cuurent time) >= 15 minutes **/
       }

Here is an example of how you can accomplish this. I made this slightly more verbose so it's easier to read.

import groovy.time.TimeCategory

def now = new Date()
def fifteenMinutesAgo = null
use(TimeCategory) {
    fifteenMinutesAgo = now - 15.minutes
}

def feedbackList = Feedback.createCriteria().list() {
  eq('isMailSent', false)
  le('createdOn', fifteeenMinutesAgo)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM