user2809386
user2809386

Reputation:

Grails create criteria not in list

how to get list A where not use in domain B?

Domain A

Class A{
String name
String code
}

Domain B

Class B{
A aaa
String description
}

example data:

**domain A**

id+versioin+name   +code+|
1 | 0      |Bobby  |bob  |
2 | 0      |anto   |ant  |
3 | 0      |Jessica|jes  |
4 | 0      |hera   |her  |

**domain B**    
id+version|a_id|description + |
1 | 0     | 1  |this is bobby |
2 | 0     | 3  |this is jessic|

how can i get list from A where not used in B.

i tried this
def b = B.list()
def c = A.createCriteria()
def results = c.list {
    not { 'in'(b) }
}

but fail..

Upvotes: 6

Views: 8706

Answers (2)

Graeme Rocher
Graeme Rocher

Reputation: 7985

For completeness, note in Grails 2.4 (which is at the RC phase and about to go GA) you can write this query as a single query using a subquery, which will perform better:

  def c = A.createCriteria()
  def results = c.list {
      notIn new DetachedCriteria(B).id
  }

Upvotes: 2

user2809386
user2809386

Reputation:

def b = B.list()
def c = A.createCriteria()
def results = c.list {
    not { 'in'("id",b*.aaa.id) }
}

Upvotes: 8

Related Questions