简体   繁体   English

如何使用 {} 在 JPA 查询中传递列表参数

[英]How to pass parameter of list in JPA query with {}

Below query is wokring in postgres SQL and giving required result, but when trying from JAVA it is giving error.下面的查询是 postgres SQL 中的 wokring 并给出所需的结果,但是当从 JAVA 尝试时,它给出了错误。

select * from res where 
'{"^\\d{3}\\d{1,}133\\d{1,}$","^\\d{3}7483"}' is null
or exists (select from unnest(cast('{"^\\d{3}\\d{1,}133\\d{1,}$","^\\d{3}7483"}' as text[])) rx where cast (r.res_value as text) ~ rx);

java code:- java代码:-

@Query(value = "select * from res where 
'{?1 is null
or exists (select from unnest(cast(?1 as text[])) rx where cast (r.res_value as text) ~ rx)",nativeQuery = true)
public List<Res> getData(List<String> patterns);

List of patterns = 
^\\d{3}\\d{1,}133\\d{1,}$
^\\d{3}7483

Error getting - "ERROR: malformed array literal: ^\\d{3}\\d{1,}133\\d{1,}$ Detail: Array value must start with "{" or dimension information.获取错误 - “错误:格式错误的数组文字:^\\d{3}\\d{1,}133\\d{1,}$ 详细信息:数组值必须以“{”或维度信息开头。

How i can add { } to these.. any solution in JPA我如何将 { } 添加到这些 .. JPA 中的任何解决方案

It's better to convert the List of String to String in java and pass it as String param to the JPA Query.最好在java中将字符串列表转换为字符串并将其作为字符串参数传递给JPA查询。

Step 1: Convert List of String to String in java before passing to JPA第 1 步:在传递给 JPA 之前在 java 中将字符串列表转换为字符串
Java: String patternsStr = patterns.stream().map(p -> "\\"" + p + "\\"").collect(Collectors.joining(",", "{", "}"));

Step 2: Change the type of parameter from List to String in @Query method第二步:在@Query方法中将参数类型从List改为String
JPA: public List<Res> getData(String patternsStr);

The postgres SQL query you mentioned is accepting regx array as string.您提到的 postgres SQL 查询接受 regx 数组作为字符串。 So it's easy to pass the regx list as string to JPA Query.所以很容易将 regx 列表作为字符串传递给 JPA 查询。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM