2017-02-15 70 views
0

我有2个domainClasses如下:格姆过滤

class Customer { 
    def name 
    static hasMany = [accounts:Account] 
} 

class Account { 
    def accountNo 
    def type 
} 

这里帐户的类型可以是“节能”,“当前”,“FD”

我想写一个标准来搜索所有那些账户类型为“Saving”,“Current”的客户。

应该是什么标准,我尝试使用下面:

def customers = Customer.createCriteria().list { 
    accounts { 
     and { 
      eq('type','Saving') 
      eq('type','Current') 
     } 
    } 
} 

但在执行时它创建内部联接即给予0的结果。

+0

会发生什么事,如果你使用的不是AND OR? –

+0

因为我想获得所有拥有这两种类型帐户的客户 –

回答

2

您可以使用or而不是通过and Y. Tarion的建议或使用in

def types = ["Savings", "Current"] 
def customers = Customer.createCriteria().list { 
    accounts { 
     "in" "type", types 
    } 
} 
+0

实际上,此查询返回具有类型“储蓄”或“当前”类型的客户,但实际上我希望拥有至少两种类型帐户的客户可能拥有多于这两种类型的账户,如“储蓄”,“当前”或“储蓄”,“当前”,“FD”等。 –

+0

好了,现在我已经得到你。 其实它有点复杂,我不确定你是否可以用标准来做到这一点。 SQL将如下所示: select * from customer where id in(select customer_accounts_id from customer_account where account_id = 1)and id in(select customer_accounts_id from customer_account where account_id = 2) 请参阅:http://stackoverflow.com/questions/ 7364969/how-to-filter-sql-results-in-a-many-through-relation –

+0

我试过按照下面的方法,但没有生成适当的输出: def results = Customer.withCriteria { accounts.each {“id”中的{ ',新的DetachedCriteria(Account).build {projection.property('customer.id') } 当量( '类型',它) } } –