使用QueryDSL比较两个数组

问题描述

在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} && 'select ... where player.business_unit_ids && '{$1}'::uuid[]
'::uuid[]",player.listofUuids,StringUtils.join(",",otherUuids)));

它返回这样的sql

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

但是执行它会引发异常:

{{1}}

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

有没有想到使用QueryDSL可以实现两个数组的比较?

解决方法

弄清楚如何使用&&重叠运算符添加所需的谓词:

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重叠运算符,因此我可以用不同的方式编写此谓词。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...