如何在 Criteria with Grails 中将嵌套属性与 class 中的属性一起使用?

How to use nested attributes together with attributes from the class in a Criteria with Grails?

我遇到这种情况:

country {
    and {
        rates{
                                and{
                                    between('effectiveDate', startDate, endDate)
                                    or {
                                        and {
                                            eq('hoursEligible', true)
                                            gt('hours', new BigDecimal(0))
                                        }
                                        and {
                                            eq('travelTimeEligible', true)
                                            gt('travel', new BigDecimal(0))
                                        }
                                        and {
                                            eq('mileageEligible', true)
                                            gt('mileage', new BigDecimal(0))
                                        }
                                        and {
                                            eq('expensesEligible', true)
                                            gt('expenses', new BigDecimal(0))
                                        }
                                    }
                                }
                            }
    }
    }

事实是:小时是来自特定 class 的属性,class 具有此命名查询。 rates 是一个列表,嵌套在我的特定 class 上的嵌套对象之一中。 当我尝试在那里使用它时,我得到:

java.lang.IllegalArgumentException: object is not an instance of declaring class

如何使用此命名查询引用 hours 属性? 还有一个问题...如果费率列表中有任何项目 return 对这种情况为真,它将 return 为真,对吗?

那是我的域名class:

class TravelDetail {

    Date date
    Country country
    BigDecimal hours
    BigDecimal mileage
    BigDecimal travel
    BigDecimal expenses

我进入的国家:

class Country {

static hasMany = [rates: Rate

我的评分是:

class Rate {

    Boolean hoursEligible = Boolean.TRUE
        Boolean travelTimeEligible = Boolean.TRUE
        Boolean mileageEligible = Boolean.TRUE
        Boolean expensesEligible = Boolean.TRUE

在每个 and 中拆分 rate 条件,像这样,

country {
    and {
        between('effectiveDate', startDate, endDate)
        or {
            and {
                rates {eq('hoursEligible', true)}
                gt('hours', new BigDecimal(0))
            }
            .
            .
            and {
                rates {eq('expensesEligible', true)}
                gt('expenses', new BigDecimal(0))
            }
        }
    }
}

我不认为我会采用像您这样的模型,但通常查询应该如下所示(前提是您针对 TravelDetails 调用它):

def list = TravelDetail.withCriteria{
  between 'effectiveDate', startDate, endDate
  or {
     and {
         country{ rates { eq 'hoursEligible', true } }
         gt 'hours', 0
     }
     ....
   }
}