【问题标题】:Compare two arrays using QueryDSL使用 QueryDSL 比较两个数组
【发布时间】:2020-10-12 21:07:09
【问题描述】:

在 QueryDSL 谓词组合方面需要帮助 - 如何编写 QueryDSL 谓词以使用 && 运算符比较两个数组(查找两个数组之间的任何 UUID 匹配项):

select '{e48f54d5-9845-4987-a53d-e0ecfe3dbb43}'::uuid[] && '{e48f54d5-9845-4987-a53d-e0ecfe3dbb43,4e9a43f2-cb23-4f1b-9f7f-c09687d97570}'::uuid[];

使用: 蟑螂 - v20.1.7, QueryDSL - v4.3.1

尝试了以下方法:

private BooleanBuilder createPredicates(QPlayer player, List<UUID> otherUuids) {
    predicates.and(player.listOfUuids.any().in(otherUuids)); // player.listOfUuids is type of ListPath<java.util.UUID, ComparablePath<java.util.UUID>>

    return predicates;
}

但它会引发异常:

java.lang.IllegalStateException: name property not available for path of type COLLECTION_ANY. Use getElement() to access the generic path element.

还尝试像这样创建booleanTemplate

predicates.and(Expressions.booleanTemplate("{0} && '{{1}}'::uuid[]", player.listOfUuids, StringUtils.join(",", otherUuids)));

它返回这样一个SQL:

select ... where player.business_unit_ids && '{$1}'::uuid[]

但是执行它会引发异常:

io.r2dbc.postgresql.ExceptionFactory$PostgresqlNonTransientResourceException: [08P01] received too many type hints: 1 vs 0 placeholders in query

因为它解释了额外的 '{' 和 '}' 需要用来将其包装在 uuid 数组中作为另一个占位符。而且它也不尊重特殊符号转义或unicode。

有没有想过如何使用 QueryDSL 实现两个数组比较?

【问题讨论】:

    标签: spring-boot querydsl cockroachdb spring-data-r2dbc


    【解决方案1】:

    想出了如何使用 &amp;&amp; 重叠运算符添加所需的谓词:

    predicates.and(Expressions.booleanTemplate("{0} && {1}::uuid[]", arg0, String.format("{%s}", arg1.stream().map(UUID::toString).collect(joining(",")))))
    

    它基于示例查询工作:

    select '{e48f54d5-9845-4987-a53d-e0ecfe3dbb43,e48f54d5-9845-4987-a53d-e0ecfe3dbb45}'::uuid[] && '{e48f54d5-9845-4987-a53d-e0ecfe3dbb40,e48f54d5-9845-4987-a53d-e0ecfe3dbb45}'::uuid[];
    

    我没有发现 QueryDSL 将支持 Ops.class 中的 &amp;&amp; 重叠运算符,我将能够以不同的方式编写此谓词。

    【讨论】:

      猜你喜欢
      • 2019-02-01
      • 1970-01-01
      • 2021-05-31
      • 1970-01-01
      • 2014-12-23
      • 2017-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多