简体   繁体   English

如何将字符串列表作为参数传递给 JPA 本机查询并在 Postgresql ARRAY []

[英]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 文本数组,查询返回预期响应。

如何传递 Map 或 List<object> 作为 JPA 查询的参数<div id="text_translate"><p>我需要将 map 作为 <Sting,String> 或 List 类型的输入参数传递到两个 JPA 列中,并将结果作为映射/列表中所有条目的记录列表。</p><p> 是这样的:</p><pre> @Query( value = "SELECT e from #{#entityName} e where e.name = KEY(someMap) and e.relationName = VALUE(someMap)") List<Member> findByNameAndRelationNameIn( @Param("someMap") Map<String, String> someMap);</pre><p> 或者</p><pre>@Query( value = "SELECT e from #{#entityName} e where e.name IN (:#{#dataSpaceMembers.?[name]}) and e.dataSpaceName IN (:#{#dataSpaceMembers.?[dataSpaceName]})") List<DataSpaceMember> findByNameAndDataSpaceIn( @Param("dataSpaceMembers") List<DataSpaceMember> dataSpaceMembers);</pre><p> 但是应用程序不会运行,因为这不是有效的 jpa 查询。 我不想在循环中运行单个查询,而是寻找一个可以将结果作为列表给出的查询。</p></div></object> - how to pass Map or List<Object> as a parameter to JPA query

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 {} 在 JPA 查询中传递列表参数 - How to pass parameter of list in JPA query with {} 如何使用JPA将数组参数传递给Postgresql函数? - how to pass an array parameter using jpa to a postgresql function? 如何在jpa查询上传递参数? - How to pass parameter on jpa query? 如何传递 Map 或 List<object> 作为 JPA 查询的参数<div id="text_translate"><p>我需要将 map 作为 <Sting,String> 或 List 类型的输入参数传递到两个 JPA 列中,并将结果作为映射/列表中所有条目的记录列表。</p><p> 是这样的:</p><pre> @Query( value = "SELECT e from #{#entityName} e where e.name = KEY(someMap) and e.relationName = VALUE(someMap)") List<Member> findByNameAndRelationNameIn( @Param("someMap") Map<String, String> someMap);</pre><p> 或者</p><pre>@Query( value = "SELECT e from #{#entityName} e where e.name IN (:#{#dataSpaceMembers.?[name]}) and e.dataSpaceName IN (:#{#dataSpaceMembers.?[dataSpaceName]})") List<DataSpaceMember> findByNameAndDataSpaceIn( @Param("dataSpaceMembers") List<DataSpaceMember> dataSpaceMembers);</pre><p> 但是应用程序不会运行,因为这不是有效的 jpa 查询。 我不想在循环中运行单个查询,而是寻找一个可以将结果作为列表给出的查询。</p></div></object> - how to pass Map or List<Object> as a parameter to JPA query Spring 数据 jpa 本机查询与 null 参数 (PostgreSQL) - Spring data jpa native query with null parameter (PostgreSQL) 如何在本机查询 JPA 中传递参数 - How to pass parameters in a native query JPA JPA 将字符串列表传递给本机查询中的 IN 子句(使用 citext) - JPA Passing a list of Strings in to IN clause in a native query (using citext) 在 JPA 中使用数组进行本机查询 - Native Query with array in JPA 如何使用Spring数据JPA在原生查询中实现列表类型的可选参数 - How to implement optional parameter of list type in native query using Spring Data JPA 如何通过 postgresql 查询在字符串列表中搜索枚举? - How to search an enum in list of strings by postgresql query?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM