如何在Grails的Criteria中使用嵌套属性和类中的属性?
我有这样的情况:如何在Grails的Criteria中使用嵌套属性和类中的属性?,grails,gorm,criteria,Grails,Gorm,Criteria,我有这样的情况: country { and { rates{ and{ between('effectiveDate', startDate, endDate) or { and {
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))
}
}
}
}
}
}
问题是:hours是一个特定类的属性,该类具有此命名查询。rates是嵌套在我的特定类上的一个嵌套对象中的列表。
当我尝试在那里使用它时,我得到:
java.lang.IllegalArgumentException: object is not an instance of declaring class
如何使用此命名查询引用hours属性?
还有一个问题。。。如果“费率”列表中的任何项目在此条件下返回true,则返回true,对吗
这是我的域类:
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
在每个
和
中拆分速率
条件,如下所示
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))
}
}
}
}
在每个
和
中拆分速率
条件,如下所示
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
)调用它):
我不认为我会采用像您这样的模型,但一般来说,查询应该是这样的(前提是您针对
TravelDetails
)调用它):
显示您的域classes@injecteer,在那里添加了部分域。显示您的域classes@injecteer,在那里添加了部分域。是的,这就是我在这里所做的,但似乎只有当列表中的所有值都符合我所做的条件时,速率块才会返回true,我需要它为true。似乎我需要做一些类似“利率,任何”的事情。如果没有它,则仅当所有项都为true时才返回对象。请尝试打印结果查询,以便更好地了解。谢谢。以前忘了回答这里。。。但它帮助并解决了我的问题。。只是需要应用一些更改。。但是我不得不多次迭代rates属性…@Igor,如果你对这个解决方案不是很满意,因为有很多次迭代;我建议您改为使用HQL。我发现,这有助于复杂的查询。是的,我在这里就是这么做的,但似乎只有当列表中的所有值都符合我所做的条件时,rates块才会返回true,我需要它为true。似乎我需要做一些类似“利率,任何”的事情。如果没有它,则仅当所有项都为true时才返回对象。请尝试打印结果查询,以便更好地了解。谢谢。以前忘了回答这里。。。但它帮助并解决了我的问题。。只是需要应用一些更改。。但是我不得不多次迭代rates属性…@Igor,如果你对这个解决方案不是很满意,因为有很多次迭代;我建议您改为使用HQL。我发现,它有助于复杂的查询。