选择grails中一个月的所有活动

时间:2009-09-23 19:52:26

标签: grails gorm

我是groovy / grails的新手,我正在尝试进行一个标准搜索,找到一个月的所有帖子,基本上是这样的:

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
}                  

域类

 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
    }

}

现在,为什么我会得到这个例外?

  

org.codehaus.groovy.runtime.InvokerInvocationException:org.hibernate.QueryException:无法解析属性:job.happening:JobRegistration

1 个答案:

答案 0 :(得分:0)

您需要嵌套工作关系(您也可以只使用我们的eq):

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

    }

    projections {
            sum("minutesWorked")
    }
}

欢呼声