Grails find entries with specific minute
I have several domain classes which generate large data sets开发者_JAVA百科 each minute. Something like this:
class Foo{
String name
Date received
int value
}
I want to accomplish the same result set this MySQL query returns:
SELECT * FROM foo WHERE received BETWEEN ? AND ? AND MINUTE(received) = ?
Is this possible using a NamedQuery or findAllBy... ?
Thanks.
Well, since HQL does provide a minute function, I was able to find a solution by myself. Since Foo is part of many other classes I wanted a common method for all of them. Anyway, here's the solution for Foo and friends:
clazz.executeQuery(
'from ' + clazz.getSimpleName() + ' c where c.received between :from and :to
and minute(received) = :period', [from:d1, to:d2, period:0])
Thanks anyway Don.
In order to avoid HQL, you can use sqlRestriction in a Criteria. It doesn't support parameters, though.
// Some arbitrarily chosen values for the query placeholders
Date minReceived = new Date()
Date maxReceived = minReceived + 5
Integer minute = 0
Foo.findAll(
"from Foo where received between :min and :max and minute(received) = :minute",
[min: minReceived, max: maxReceived, minute: minute])
精彩评论