[英]How to pass List of strings as parameter to JPA native query and substitute the same in Postgresql ARRAY[]
I have a JPA native query which looks something like this我有一个看起来像这样的 JPA 本机查询
@Query(
value = "SELECT * FROM feature f where jsonb_exists_any(f.feature_json -> 'properties' -> 'ids', array[:ids])",
nativeQuery = true)
feature_json is a jsonb column and:ids is the input param I need to pass to the query which would be a List of UUID Strings feature_json 是一个 jsonb 列,并且:ids 是我需要传递给查询的输入参数,它是一个 UUID 字符串列表
Query with sample values is as below带有样本值的查询如下
SELECT * FROM feature f where jsonb_exists_any(f.feature_json -> 'properties' -> 'ids', array['79eeb7ae-c2d0-3344-a6f5-eb424b820d03','a14bdb38-cedb-3743-bd50-9562ed2856b2'])
When I pass the above values as a List of strings in Java, the query fails to execute as the value in:ids is substituted as (79eeb7ae-c2d0-3344-a6f5-eb424b820d03,a14bdb38-cedb-3743-bd50-9562ed2856b2)
instead of the required '79eeb7ae-c2d0-3344-a6f5-eb424b820d03','a14bdb38-cedb-3743-bd50-9562ed2856b2'
当我将上述值作为 Java 中的字符串列表传递时,查询无法执行,因为:ids 中的值被替换为
(79eeb7ae-c2d0-3344-a6f5-eb424b820d03,a14bdb38-cedb-3743-bd50-9562ed2856b2)
所需'79eeb7ae-c2d0-3344-a6f5-eb424b820d03','a14bdb38-cedb-3743-bd50-9562ed2856b2'
I also tried to pass this quoted comma separated values as a string to:ids parameter, the query executes without any errors but does not fetch any results.我还尝试将此引用的逗号分隔值作为字符串传递给:ids 参数,查询执行时没有任何错误,但不获取任何结果。
How to pass a list of uuid strings from Java as a parameter to the native query as specified above?如上所述,如何将来自 Java 的 uuid 字符串列表作为参数传递给本机查询?
UPDATE:更新:
I modified the query as below:我将查询修改如下:
@Query(
value = "SELECT * FROM feature f where jsonb_exists_any(f.feature_json -> 'properties' -> 'ids', :ids)",
nativeQuery = true)
List<FeatureEntity> getFeaturesByIds(@Param("ids") TypedParameterValue ids);
And from the invoking function, passed in new TypedParameterValue(StringArrayType.INSTANCE, stringArray)
并从调用 function 中传入
new TypedParameterValue(StringArrayType.INSTANCE, stringArray)
The stringArray
is cast into postgresql text array during query execution and the query returns expected response. stringArray
在查询执行期间被转换为 postgresql 文本数组,查询返回预期响应。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.