子级连接两级父表的命名查询

Named query on child joining two levels of parent tables

给出

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]

}

我收到一个错误 运行:

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)

使用命名查询完成此任务的正确方法是什么?我能让它工作的唯一方法是使用 createCriteria 并为父表声明关节。

这可能是因为 store 已在您的命名查询中定义。尝试在命名查询中更改此变量的名称,例如

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