2016-11-07 51 views
1

我有这种情况:如何将嵌套属性与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)) 
             } 
            } 
           } 
          } 
    } 
    } 

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

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

如何我指的是时间属性使用此命名查询? 另外还有一个问题......如果在费率列表中有任何项目返回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 
+0

显示你的域类 – injecteer

+0

@injecteer,在那里增加了部分域名。 – Igor

回答

0

分割在每一个andrate条件,东西像这样,

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)) 
      } 
     } 
    } 
} 
+1

是的,这就是我在这里所做的,但似乎只有当列表中的所有人对我所做的条件都是真实的,并且我需要它是真实的时,速率模块才会返回true。似乎我需要做一些类似“rates.any”的事情。没有它,只有在所有项都为真时才返回对象 – Igor

+0

尝试打印生成的查询,以获得更好的想法。 –

+0

谢谢。忘了在这里回答...但它帮助和解决了我的问题..只需要应用一些更改..但我不得不迭代进入rates属性多次... – Igor

0

我不认为,我会采用一种模式像你这样的,但一般的查询应该像(前提是你叫它对TravelDetails):

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