Spring Data本机查询不允许Postgres jsonb字符串存在运算符(问号) [英] Spring data native query does not allow Postgres jsonb string exist operator (question mark)

查看:107
本文介绍了Spring Data本机查询不允许Postgres jsonb字符串存在运算符(问号)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to user Postgres jsonb string exist operator in the SpringData native query.

SpringData方法示例:

SpringData method example:

@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 在数据库。
我尝试用 \\ 排除问号,但仍然出现以下错误:
org.postgresql。 util.PSQLException:没有为参数2指定值。

Where worker_idsis of type JSOB in the database. I've tried to exclude question mark with \\ but still got below error : org.postgresql.util.PSQLException: No value specified for parameter 2.

是否可以使用此运算符与spring数据本机查询?

Is there a way to use this operator with spring data native query?

推荐答案

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

All operators in PostgreSQL uses underlying procedure:

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

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

因此您可以使用重写查询jsonb_exists(jsonb,text)像这样:

So you can rewrite your query using jsonb_exists(jsonb, text) like this:

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

这篇关于Spring Data本机查询不允许Postgres jsonb字符串存在运算符(问号)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆