grailscreatecriteria

How to distinct list from createCriteria?


I have a problem ..to distinct my list..this is my code..

domain :

class City {
    String city
    static constraints = {
        city(blank:false,unique:false)
    }
}

class Financial {
    String financial
    String description
    static constraints = {
        financial(blank:false,unique:false)
    }
}

class Bank {
    Financial financial
    City city
    static constraints = {
        financial(blank:false)
    }
}

I want to create a list from domain bank, with this code :

def index= {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)

        if(!params.sort && !params.order)
        {
            params.sort = "city"
            params.order = "desc"
        }
        def c = Bank.createCriteria()
        def results = c.list(params)
        {
            if(params.financial)
            {
                financial{                  
                    ilike("financial", "%${params.financial}%")s
                }   

            }

        }

        [bankdetaillist: results,bankdetaillisttotal:results.totalCount, financial: params.financial?:""]
    }

If i create a bank , example..

Table bank :

id | version | city | financial
-------------------------------
 0 |    0    |   1  |    1        
 1 |    0    |   5  |    1

From this case, we know the bank with financial 1 have many cities...

And I want to show it to list with a distinct bank from field financial.


Solution

  • A createCriteria seems a bit overkill for what you want. What about:

    Financial finRecord = Financial.findByFinancial(params.financial)
    List<Bank> banksForFin = Bank.findAllByFinancial(finRecord,[sort: "city", order: "desc"])