2012-03-26 65 views
2

枚舉集查詢關聯我試圖在Grails的1.2.1查詢,發現所有產品均由租戶類型。與Grails中

我的解決方案的工作,但是是非常低效的,首先我檢索所有產品,然後找到一個給定的承租人所有匹配的結果。

我發現JIRA相關的bug:Enum as collection

class Product { 
    Set<TenantType> tenants 
    static hasMany = [tenants: TenantType] 
} 

enum TenantType { 
    BICYCLE, 
    MOTORCYCLE 
} 

def tenant = TenantType.BICYCLE 
Product.list().findAll { product -> tenant in product.tenants } 

有查詢該數據的更有效的方法?

+0

[Grails選擇域對象基於枚舉列表屬性中的枚舉值](http://stackoverflow.com/questions/4829823/grails-select-domain-objects-based -on-AN-枚舉值在-AN-枚舉列表屬性) – 2015-10-06 10:25:24

回答

3

類似的問題被問到here,正如答案中指出的那樣,它看起來像Hibernate不支持像枚舉這樣的值類型集合的條件查詢。一種選擇是用一個HQL查詢,而不是:

Product.executeQuery('from Product p inner join p.tenants tenants 
         where tenants = :tenant', [tenant: TenantType.BICYCLE]) 
0

可以不執行加入:

Product.executeQuery('from Product where :tenant in elements(tenants)', [tenant: TenantType.BICYCLE])