小编典典

Spring Data本机查询不允许Postgres jsonb字符串存在运算符(问号)

hibernate

我正在尝试在SpringData本机查询中使用Postgres jsonb字符串存在运算符。

SpringData方法示例:

@Query(value = "SELECT t.id \n"
        + " FROM task AS t \n"
        + " WHERE (t.worker_ids \\? :workerId)\n"
        + " ORDER BY t.created_at\n",
        nativeQuery = true)
Optional<String> findMatchingTaskId(@Param("workerId") String workerId);

worker_ids数据库中,哪里是JSOB类型。我试图用排除问号,\\但仍然出现以下错误:
org.postgresql.util.PSQLException: No value specified for parameter 2.

有没有办法将此操作符与spring数据本机查询一起使用?


阅读 439

收藏
2020-06-20

共1个答案

小编典典

PostgreSQL中的所有运算符都使用基础过程:

> SELECT oprname, oprcode FROM pg_operator WHERE oprname LIKE '%?%'

oprname | oprcode
--------------------------
?       | jsonb_exists
?|      | jsonb_exists_any
?&      | jsonb_exists_all
...

因此,您可以使用以下方式重写查询jsonb_exists(jsonb, text)

SELECT t.id
FROM task AS t
WHERE jsonb_exists(t.worker_ids, :workerId)
ORDER BY t.created_at
2020-06-20