[英]How can I use all(array[]) in a native query with Spring Boot?
我尝试使用 Spring Boot using all(array[])
function 进行本机查询,但是,我无法正确完成。 我不知道我将传递的字符串数量,它是一个动态数量。 你们能帮我解决这个问题吗?
我试过使用List<String>
、 String[]
和String
如下:
传递String
并在查询中all(array[:texto])
:没有错误,但是没有结果。
传递List<String>
并在查询中all(array[:texto])
:
org.postgresql.util.PSQLException: ERROR: operator does not exist: character varying ~~* record
Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.
String[]
并在查询中all(array[:texto])
:org.postgresql.util.PSQLException: ERROR: operator does not exist: character varying ~~* bytea
Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.
String[]
并在查询中all(array[CAST(:texto AS TEXT)])
:没有错误,但是,没有结果。@Query(value="SELECT * FROM Tag WHERE nome ILIKE all(array[:texto])", nativeQuery=true)
public List<Tag> findPacotesByTexto(@Param("texto") List<String> texto);
编辑:
@Entity
public class Tag {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(nullable = false)
private String nome;
@ManyToOne
@JsonIgnore
@JoinColumn(name="pacote_id")
private Pacote pacote;
Tag() {
}
public Tag (String nome, Pacote pacote) {
this.nome = nome;
this.pacote = pacote;
}
public long getId() {
return id;
}
public String getNome() {
return nome;
}
public Pacote getPacote() {
return pacote;
}
}
我怎样才能使这项工作?
提前致谢。
在本机查询方法中,将完全传递SQL以照原样执行。 而且我认为该框架无法将您的集合序列化为所需的模式all(array[?, ?, ?, ...])
。 在这种情况下,您应该在传递给方法之前转换参数,或者将方法更改为不使用本机查询。
使用 spring + hibernate & 本机查询将列表转换为文本 [] 时遇到同样的问题
有关工作解决方案,请参阅https://stackoverflow.com/a/55179555/335264 (使用两个 postgresql 函数进行转换)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.