spring - 如何在 JPA 中参数化 jsonb_path_query
问题描述
使用 nativeQuery 作为 name_id 属性将参数传递到 jsonb_path_query 的正确语法是什么?
@Query(value = "select jsonb_path_query(columns , '$.names[*] ? (@.name_id == :id && @.is_active == true)' ) -> 'name_id' as name
解决方案
jsonb_path_query()
接受一个额外的 JSONB 参数,该参数可以包含变量作为键/值对。$
可以使用JSON 路径中的符号来引用键:
jsonb_path_query(columns , '$.names[*] ? (@.name_id == $id && @.is_active == true)', '{"id": 42}')
jsonb 值可以作为 JDBC 参数传递:
"select jsonb_path_query(columns , '$.names[*] ? (@.name_id == $id && @.is_active == true)', cast(? as jsonb) ) -> 'name_id' as name"
然后,您可以将 String 参数传递"{\"id\": 42}"
给查询。
我不知道 JPA,在普通 JDBC 中,这将与PreparedStatement and
setString(1, "{"id": 42}")`一起使用
推荐阅读
- lua - 如何在一段时间内保持一个值
- kubernetes - 如何将 Istio-ingressgateway 类型从“LoadBalancer”更改为“ClusterIP”
- python - 501 无效请求
- angular - 错误:发生未处理的异常:对象原型可能只是一个对象或 null:从 Angular 7 迁移到 9 时未定义
- excel - 在工作簿中循环通过 Excel 工作表
- python - Streamlit 输入更改时的重置结果
- java - 使用二进制搜索查找旋转点,为什么要与第一个元素进行比较?
- vuetify.js - Vuetify:单击 v-tab 中的选项卡时更新 URL
- xml - XPath 后代属性过滤器包含
- go - golang:使 select statemet 具有确定性?