繁体   English   中英

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

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

下面的查询是 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代码:-

@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

获取错误 - “错误:格式错误的数组文字:^\\d{3}\\d{1,}133\\d{1,}$ 详细信息:数组值必须以“{”或维度信息开头。

我如何将 { } 添加到这些 .. JPA 中的任何解决方案

最好在java中将字符串列表转换为字符串并将其作为字符串参数传递给JPA查询。

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

第二步:在@Query方法中将参数类型从List改为String
JPA: public List<Res> getData(String patternsStr);

您提到的 postgres SQL 查询接受 regx 数组作为字符串。 所以很容易将 regx 列表作为字符串传递给 JPA 查询。

暂无
暂无

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

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