我正在尝试在 Grails 1.2.1 中进行查询,按租户类型查找所有产品。
我的解决方案有效但效率很低,首先我检索所有产品,然后找到给定租户的所有匹配结果。
我在 JIRA 中发现了一个相关的错误: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 }
是否有更有效的方式来查询这些数据?
最佳答案
有人问了类似的问题here正如答案中指出的那样,Hibernate 似乎不支持对枚举等值类型集合的条件查询。一种选择是改用 hql 查询:
Product.executeQuery('from Product p inner join p.tenants tenants
where tenants = :tenant', [tenant: TenantType.BICYCLE])
关于grails - 在 Grails 中查询与枚举集合的关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9869442/