Named query on child joining two levels of parent tables

251 Views Asked by At

Given

class Store {
    String name

    static hasMany = [departments: Department]
}

class Department {

    String name

    static belongsTo = [store: Store]

    static hasMany = [products: Product]

}

class Product {
    String name
    Integer qty

    static namedQueries = {
        productsInStockByStore {store->
            department {
                store {
                    eq 'id', store.id
                }
            }
            gt 'qty', 0
        }
    }

    static belongsTo = [department: Department]

}

I get an error running:

def store = Store.first() // just for the sake of testing
Product.productsInStockByStore(store).list()

No signature of method: namedboom.Store.call() is applicable for argument types: (namedboom.Product$__clinit__closure1$_closure3$_closure4$_closure5) values: [namedboom.Product$__clinit__closure1$_closure3$_closure4$_closure5@768aab6a] Possible solutions: wait(), last(), save(), any(), getAll(), wait(long)

What would be the right way to accomplish this with named query? The only way I could get it to work is using createCriteria and declaring joints for the parent tables.

1

There are 1 best solutions below

1
On BEST ANSWER

This is may be because store is defined in your named query. Try changing name of this variable in the named query, like

static namedQueries = {
    productsInStockByStore {storeInstance->
        department {
            store {
                eq 'id', storeInstance.id
            }
        }
        gt 'qty', 0
    }
}