Question

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

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top