I'm upgrading a Grails app from 2.2.4 to 2.5.0, and from Hibernate 3 to Hibernate 4.
There is an existing GORM findAll
query that passes a java.util.Date
property (named start
) of the domain object to a call to a static method that returns a copy of the Date
with the time component stripped out.
Here's a simplified version of the findAll
call (excluding criteria other than the one that throws the exception):
Reservation.findAll {
DateUtils.justDate(start) == DateUtils.justDateToday()
}
This runs without any exceptions in 2.2.4, but, in 2.5.0, it throws a GroovyCastException
saying that start
is a grails.gorm.DetachedCriteria
, and cannot be cast to java.util.Date
.
How can I get the query to work?
I could use something like:
Reservation.findAll {
start >= DateUtils.justDateToday() &&
start < DateUtils.justDateTomorrow()
}
But that seems inelegant. Also, domain object properties might be used as arguments to static methods in other findAll
closures, so a generic solution to this issue will still be useful.
Using where
query you can use methods like date()
, month()
and year()
as below:
import static java.util.Calendar.*
Date today = new Date()
Reservation.where {
year(start) == today[YEAR] &&
month(start) == today[MONTH] &&
day(start) == today[DAY_OF_MONTH]
}.list()