2015-06-09 90 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