2014-09-24 54 views
2

獲取域對象所有屬性的映射(排除關聯)的最佳方式是什麼?特別是我需要排除hasMany協會域對象屬性列表,排除關聯

現在,我硬連線的屬性列表中排除(集合):

def p = someBook.properties 
p = p.subMap(p.keySet() - ['authors', 'editors', 'formats']) 

我並確保properties返回一個懶惰集合,只要subMap()和keySet()是關心的,所以排除的屬性實際上並沒有從DB中獲取。

但我寧願不硬編碼關聯列表。

回答

1

通過丟棄properties中的鍵值對排除所有關聯。

someBook.properties.findAll { !(it.key in someBook.hasMany?.keySet()) } 
+0

'someBook.hasMany'!太精彩了!!很明顯,現在你提到它。謝謝 – Tobia 2014-09-24 19:58:17

+0

我會將你的建議與我的'subMap'合併,因爲'findAll'也會爲排除的屬性執行數據庫查詢 – Tobia 2014-09-24 19:59:42

相關問題