grails - 在 Grails 中查询与枚举集合的关联

标签 grails hql grails-orm criteria

我正在尝试在 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/

相关文章:

grails - 告诉我有没有脏东西?

grails - 在 IntelliJ 中启用 Grails 3.x 自动重新加载

Grails-ish 用不同网络上的电子邮件为用户建模的方法

mysql - JPQL 内连接

java - HQL 按日期查询(Java+NetBeans)

grails - 如何在 Grails 中对存储过程记录进行建模?

grails - 将请求参数绑定(bind)到过滤器中的域对象

rest - 测试API Grails

mysql - 在 Mysql 中加入 HQL 更新的替代方法

chalice : how to customize order with a sql function?