简体   繁体   English

如何在带有 Spring Boot 的本机查询中使用 all(array[])?

[英]How can I use all(array[]) in a native query with Spring Boot?

I've tried to do a native query with Spring Boot using all(array[]) function, however, I couldn't make it correctly.我尝试使用 Spring Boot using all(array[]) function 进行本机查询,但是,我无法正确完成。 I don't know the quantity of Strings that I'll pass, it's a dynamic quantity.我不知道我将传递的字符串数量,它是一个动态数量。 Can you guys help me on this, please?你们能帮我解决这个问题吗?

I've tried using List<String> , String[] and just String as below:我试过使用List<String>String[]String如下:

  1. passing String and in the query all(array[:texto]) : no errors, however, no results.传递String并在查询中all(array[:texto]) :没有错误,但是没有结果。

  2. passing List<String> and in the query 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.
  1. passing String[] and in the query all(array[:texto]) :传递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.
  1. passing String[] and in the query all(array[CAST(:texto AS TEXT)]) : no errors, however, no results.传递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);

EDITED:编辑:

@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;
    }

}

How can I make this work?我怎样才能使这项工作?

Thanks in advance.提前致谢。

In a native query approach, the SQL is passed exactly to be executed as is what it's written. 在本机查询方法中,将完全传递SQL以照原样执行。 And I think the framework could not serialize your collection as the required pattern all(array[?, ?, ?, ...]) . 而且我认为该框架无法将您的集合序列化为所需的模式all(array[?, ?, ?, ...]) In that case, you should convert your parameter before passing to the method or change the approach to not using the native query. 在这种情况下,您应该在传递给方法之前转换参数,或者将方法更改为不使用本机查询。

Had the same trouble casting the list to a text[] using spring + hibernate & native query使用 spring + hibernate & 本机查询将列表转换为文本 [] 时遇到同样的问题

See https://stackoverflow.com/a/55179555/335264 for a working solution (uses two postgresql functions to do the casting)有关工作解决方案,请参阅https://stackoverflow.com/a/55179555/335264 (使用两个 postgresql 函数进行转换)

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

相关问题 如何在 Spring Boot 的查询注释中使用我的参数? - How can I use my parameter in query annotation in Spring Boot? 我如何在 spring 引导中编写本机查询,并在 PostgreSQL 中使用连接子句 - how can i write a native query in spring boot whith join clause in PostgreSQL 本机查询在 spring 启动时返回#pageable 的所有记录 - Native query is returning all the records for the #pageable in spring boot 如何在Spring Boot中逐出所有缓存? - How can I evict ALL cache in Spring Boot? 我如何获得在 spring boot 中具有角色的所有用户的列表? - How can i get the liste of all users with roles in spring boot? 如何打印 Spring Boot 加载的所有配置? - How can I print all the configurations loaded by Spring Boot? 如何使用 Spring 引导在 JPA 存储库中编写 SQL 查询? - How can I write SQL query in JPA repository with Spring Boot? 如何在Spring Boot中注册并使用jackson AfterburnerModule? - How can I register and use the jackson AfterburnerModule in Spring Boot? 如何在Spring Boot中将YAML属性与构造函数注入配合使用? - How can I use YAML properties with constructor injection in Spring Boot? 如何在Spring Boot中使用其他DTO减少if语句 - How can I use other DTO to reduce the if statements in Spring boot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM