3
votes

I have a domain in Grails that is Case and other that is Payment.

I want to obtain the Cases that all Payments that is in the payments List( That is contained on the hasMany relationship), has the same collectorNick.

Example:

payment1 (collectorNick:a)  
payment2 (collectorNick:a) 
payment3 (collectorNick:b) 


Case1(payments[payment1,payment2]
Case2(payments[payment1,payment3]

When search for collectorNick:a the result will be Case1 because both payment collectorNick is a and Case2 has payment3 and the collector of payment3 is b.

I have tried to achieve that, but when the createCriteria that I have created I obtain both Case1 and Case2

I know that can be done in different ways, but I need to do with createCriteria from Case, because I need later to filter from some other fields of cases.

Case has

List<Payment> payments
static hasMany = [ payments: Payment ]

and Payment has

String collectorNick

And the createCriteria that I use is:

 def cases = Case.createCriteria().list(max: limit, offset: offset){ 
               and{  
                   payments { 
                           eq('collectorNick', params.collector)
                   }
               }
             }
1

1 Answers

4
votes

If I understand correctly what you are trying to do (find only Case instances where all payments were collected by a given collector), how about:

def cases = Case.createCriteria().list(max: limit, offset: offset){
    not {
        payments {
                ne('collectorNick', params.collector)
        }
    }
}