我想使用 JPA 标准 api 调用 postgresql 函数jsonb_path_exists
(https://www.postgresql.org/docs/12/functions-json.html)。
假设我有以下查询:
select id from person where jsonb_path_exists(person.json,'$.some_property[0].typ ? (@ =="Something")');
Run Code Online (Sandbox Code Playgroud)
通过 CriteriaBuilder 我会做类似的事情:
var jsonQuery = jsonPath + " ? (@ ==\"" + value + "\")"; // evaluates to '$.some_property[0].typ ? (@ =="Something")'
criteriaBuilder.function("jsonb_path_exists",
String.class,
entityRoot.get("json"),
criteriaBuilder.literal(jsonQuery)
)
.in(Boolean.TRUE);
Run Code Online (Sandbox Code Playgroud)
但是我还没有弄清楚如何将作为字符串提供的 jsonQuery 转换为 postgresjsonpath
类型。因此我收到以下异常:
org.postgresql.util.PSQLException: ERROR: function jsonb_path_exists(jsonb, character varying) does not exist
Run Code Online (Sandbox Code Playgroud)
正确的签名是
jsonb_path_exists(target jsonb, path jsonpath [, vars jsonb [, silent bool]])
所有带有jsonpath
参数的函数都存在此问题,例如jsonb_path_query …