1

我有以下表结构:

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

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

存在从 A->B 的一对多关系

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
4

1 回答 1

0

试试这个,显然未经测试,但可能会给你一些开始

def a = A.createCriteria()
a.list {
 createAlias("bSet", "b", CriteriaSpecification.LEFT_JOIN)
 not {
    'in'("b",['excluded_value_1','excluded_value_2'])
 }
}
于 2013-03-01T23:42:19.090 回答