2013-03-01 25 views
1

我有以下表結構:如何實現在一個子查詢計數

TABLE A: 
ID 
COL1 
COL2 
... 
COL(n) 

TABLE B: 
ID 
A_ID (id in table A) 
VALUE 

有從A-一個一對多的關係>乙

class A { 
    int id 
    ... 
    coln 
    Set<String> bSet 

    static hasMany = [bSet: B] 

    static mapping = { 
     restrictions joinTable: [name: "B", key: "A_ID", column: "VALUE"] 
    } 
} 

我怎樣才能建立一個標準,使之執行查詢,例如如下:

select table1.* from A table1 where (select count(*) from B table2 where table2.A_ID = table1.ID and table2.VALUE in ('excluded_value_1','excluded_value_2')) = 0 

回答

0

試試這個,顯然未經測試,但可能給你的東西,從開始

def a = A.createCriteria() 
a.list { 
createAlias("bSet", "b", CriteriaSpecification.LEFT_JOIN) 
not { 
    'in'("b",['excluded_value_1','excluded_value_2']) 
} 
}