Omar Faruq
Omar Faruq

Reputation: 1220

Grails 2.1 SQL to Grails GORM Query

I want something like this

'SELECT * FROM MyDomain WHERE endDate >= date AND startDate <= date'

Now please give possible solution by this way

def date = new Date().clearTime(), 
obj = MyDomain.find(....................) or
obj = MyDomain.findWhere(....................)

but i do not want any sql or hql injection. Can anybody help me.I want this by GORM. Apology for my bad English

Upvotes: 1

Views: 972

Answers (2)

dmahapatro
dmahapatro

Reputation: 50265

The easiest way to use criteria in this case will be

def myDomainList = MyDomain.withCriteria{
   def date = new Date().clearTime()
   le('startDate', date)
   ge('endDate', date)
}

Please note the rectification: The query demands less than equals and greater than equals, in which case 'le' and 'ge' has to be used in the criteria.

AND is implicit in criteria as well.

Upvotes: 1

sanghavi7
sanghavi7

Reputation: 754

You need yo write this

def myDate = new Date().clearTime()
obj = MyDomain.findAll("FROM MyDomain Where (:myDate) between startDate AND endDate", [myDate: myDate])

Or

obj = MyDomain.executeQuery("FROM MyDomain Where (:myDate) between startDate AND endDate", [myDate: myDate])

if you want to go for without sql or hql then go for criteria

 obj = MyDomain.createCriteria().list{

      and{
           lt('startDate', myDate)
           gt('endDate', myDate)
         }
}

for more information you can visit this here or here

Upvotes: 1

Related Questions