2015-06-09 31 views
0

鉴于命名的儿童加入查询父表的两个级别

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() 

法无签名:namedboom.Store.call()适用对于 参数类型: (namedboom.Product $ __ clinit__closure1 $ _closure3 $ _closure4 $ _closure5) values: [namedboom.Product $ __ clinit__closure1 $ _closure3 $ _closure4 $ _clos ure5 @ 768aab6a] 可能的解决方案:等待(),最后(),保存(),任(),GETALL(), 等待(长)

什么是与名为做到这一点的正确方法查询?我可以使用它的唯一方法是使用createCriteria并为父表声明关节。

回答

0

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

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

在这种情况下,会出现另一个问题。为什么一小时前我不问这个? ..尴尬..谢谢! – Micor