【问题标题】:Querying associations with enum collections in Grails在 Grails 中查询与枚举集合的关联
【发布时间】:2012-03-26 09:14:36
【问题描述】:

我正在 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 }

有没有更有效的方法来查询这些数据?

【问题讨论】:

标签: grails hql grails-orm criteria


【解决方案1】:

here 提出了一个类似的问题,正如答案中所指出的,Hibernate 似乎不支持对枚举等值类型集合的条件查询。一种选择是改用 hql 查询:

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

【讨论】:

    【解决方案2】:

    可以在没有join的情况下执行:

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

    【讨论】:

      【解决方案3】:

      在 Product 类中使用命名查询,例如

      findByTenantType { tenantType->
              tenants{ eq 'value', tenantType}
      
          }
      

      然后像这样访问这个命名查询 -

      def product = Product .findByTenantType(TenantType.BICYCLE).get()
      

      查看类似的博客 - http://www.grailsbrains.com/search-parent-through-child-in-aggregation-relationship/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-11-05
        • 1970-01-01
        • 2013-01-25
        • 2011-01-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-24
        相关资源
        最近更新 更多