class Animal{ ... }
class Tiger extends Animal { ... }
class Deer extends Animal { ... } 

Now I wanted to have a namedQuery as

class Animal {
        ...
    static namedQueries = {
        findAllAnimalBySpecies{
         ... some logic for fetch only Tiger
         ... some logic for fetch only Deer
        }
    }
}

In controller,

def tigers = Animal.findAllAnimalBySpecies().list()

How is it possible to fetch only "Tiger"s by querying the Animal? Or i am missing proper concept.

1

There are 1 answers

0
helgew On

Give this a try:

class Animal {
    ...
    static namedQueries = {
        findAllAnimalBySpecies { species ->
            switch(species) {
                case 'tiger':
                    Tiger.all
                    break
                case 'lion':
                    Lion.all
                    break
                ...
            }
        }
    }
}

and then: def tigers = Animal.findAllAnimalBySpecies('tiger').list()