1

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.

Isammoc
  • 883
  • 5
  • 20

1 Answers1

0

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"])
shuttsy
  • 1,585
  • 2
  • 19
  • 34
  • like this?? def financial = Financial.findByFinancial(params.financial) List banksForFin = Bank.findAllByFinancial(finRecord,[sort: "city", order: "desc"]) [bankdetaillist: banksForFin,bankdetaillisttotal:banksForFin.totalCount, financial: params.financial?:""] –  Feb 24 '14 at 09:03
  • You'll need banksForFin.size() instead of the totalCount. Also, substitute in your params.sort/order into the findAllBy parameter map instead of my hard coded values, if required. – shuttsy Feb 24 '14 at 10:09
  • can i use executeQuery? –  Feb 25 '14 at 03:20
  • Probably. But why would you want to when the dynamic finder does the trick? Generally, the best practice recommended (from my own experience and that I've seen of other Grails developers on StackOverflow) is to use dynamic finders where possible, then createCriteria for more complex queries or return sets. Suggets you consult the Grails reference docs to see what the differences are between then and the pros/cons of using executeQuery. – shuttsy Feb 25 '14 at 08:01