tags:

views:

176

answers:

1

I am new to groovy/grails, and I'm trying to to do a criteria search that finds all posts for a month, basically like this:

def getUserMinutesForYear(User user, Date date){

    Date firstDate = new GregorianCalendar(date.year, Calendar.JANUARY, 1, 0, 0, 0).time
    Date lastDate = new GregorianCalendar(date.year, Calendar.DECEMBER, 31, 23, 59, 59).time

    def c = JobRegistration.createCriteria()

    def minutes = c.get {
     and{
      eq("user.id", user.id)
      between("job.happening", firstDate, lastDate)

     }

     projections {
      sum("minutesWorked")
     }
    }



    return minutes
}

The domain classes are

 class Job {

     String title
     String description
     Date   happening

     static hasMany = [registrations:JobRegistration]
 }


class User {
    static hasMany = [authorities: Role, registrations: JobRegistration]
    static belongsTo = Role
    String username
}

class JobRegistration {

    Job job
    User user

    Integer minutesWorked
    static belongsTo = [user:User,job:Job]

    static constraints = {
     user(blank: false)
     job(blank:false)
     minutesWorked(nullable :true)
    }

    String toString(){
     return user.userRealName
    }

}

Now, why do I get this exception?

org.codehaus.groovy.runtime.InvokerInvocationException: org.hibernate.QueryException: could not resolve property: job.happening of: JobRegistration

A: 

You need to nest the job relationship (you can also just us eq with user):

def minutes = c.get {
    and{
            eq("user", user)
            job{
                between("happening", firstDate, lastDate)
            }

    }

    projections {
            sum("minutesWorked")
    }
}

cheers

Lee

leebutts
That works. What confused me in the first place was that something like eq ("user.id",user.id)worked while it didn't work for job.happening. In hindsight I can see that user.id might be represented as a primitive int while job.happening is an object.
Erik Itland