【问题标题】:Grails: query or criteria against a string/value pairs map propertyGrails:针对字符串/值对映射属性的查询或条件
【发布时间】:2014-04-16 14:14:18
【问题描述】:

Grails 提供了创建简单字符串/值映射属性部分"Maps of Objects", first paragraph 的可能性。

我想知道,以后有没有办法使用 map 属性作为查询的一部分来查询域类(使用 Gorm 动态查找器、条件或 HQL)(即为键 X 添加条件以具有值 Y )?

【问题讨论】:

    标签: grails


    【解决方案1】:

    在玩了一会儿之后几乎放弃了,我发现 map 语法(令人惊讶地)可以在 HQL 中工作。假设类看起来像:

    class SomeClass {
       Map pairKeyProperty
    }
    

    您可以构建如下所示的查询:

    select * from SomeClass sc where sc.pairKeyProperty['someKey'] = 'someValue' and sc.pairKeyProperty['someOtherKey'] = 'someOtherValue'
    

    相当整洁!我仍然更喜欢使用标准,因为它们编写起来更简洁,但它们似乎不支持相同的语法(或者我找不到它)。

    我在 GitHub 中创建了一个示例应用程序: https://github.com/deigote/grails-simple-map-of-string-value-pairs

    可以访问: http://grails-map-of-string-pairs.herokuapp.com/

    【讨论】:

      【解决方案2】:

      上面的表格使用了交叉连接。强制内部连接使用

      join sc.pairKeyProperty pk1 on index(pk1) = 'someKey'
      where 'someValue' in elements(pk1)
      

      【讨论】:

      • 最好使用“member of”而不是“elements:” join sc.pairKeyProperty pk1 on index(pk1) = 'someKey' where 'someValue' member of pk1 有谁知道如何强制执行内部加入并仍然有比较运算符(“like”、“ilike”)而不仅仅是成员资格测试?
      • 嘿jphiloon,谢谢你的回答!我想知道当交叉连接正确使用“where”时,两个连接之间是否存在任何性能差异 - 我最后的理解是没有,但也许我错了。
      猜你喜欢
      • 2017-06-10
      • 1970-01-01
      • 2011-09-14
      • 2019-03-10
      • 2019-05-11
      • 1970-01-01
      • 2018-01-22
      • 2011-09-13
      • 1970-01-01
      相关资源
      最近更新 更多