2017-02-15 63 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') } 當量( '類型',它) } } –