1

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.

XDR
  • 4,070
  • 3
  • 30
  • 54
  • You have to also show how `start` is orchestrated to figure out why does it complain about it being a `DetachedCriteria` – dmahapatro Jul 03 '15 at 16:20
  • What do you mean by "how `start` is orchestrated"? `start` is a standard GORM domain object property, declared like: `Date start`. Is that the info you're looking for? – XDR Jul 03 '15 at 17:13
  • `start` also has the following GORM constraint: `start validator: { val, obj -> !val || !obj.end || val < obj.end }`, but I doubt that a validator should affect read usage in a `findAll` closure. – XDR Jul 03 '15 at 17:17
  • Yes now I get it. I believe you cannot use utilities on domain properties like that. Refer my answer for a viable option/alternative. – dmahapatro Jul 03 '15 at 17:32

1 Answers1

0

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()
dmahapatro
  • 49,365
  • 7
  • 88
  • 117