是否可以在Spring Repository@Query annotation中将数组对象用作参数?

是否可以在Spring Repository@Query annotation中将数组对象用作参数?,spring,postgresql,spring-data-jpa,Spring,Postgresql,Spring Data Jpa,是否可以在Spring Repository@Query annotation中将数组对象用作参数 我正在尝试检索列节点位于字符串数组中的表中的所有行。可以在Spring存储库中使用@Query注释一次完成吗 这是我的位置实体: @Entity @Table(name = "LOCATIONS") public class Location extends Measurement{ private String latitude; private String nsIndica

是否可以在Spring Repository@Query annotation中将数组对象用作参数

我正在尝试检索列节点位于字符串数组中的表中的所有行。可以在Spring存储库中使用@Query注释一次完成吗

这是我的位置实体:

@Entity
@Table(name = "LOCATIONS")
public class Location extends Measurement{

    private String latitude;
    private String nsIndicator;

    private String longitude;
    private String ewIndicator;

    @ManyToOne
    @JoinColumn(name="node")
    private Node node;
}
其中node引用node类,它在数据库中作为BIGINT映射

我有这样一个存储库:

public interface LocationRepository extends CrudRepository<Location, Long>{

    @Query(value=
            "SELECT l1.node, l1.id, l1.latitude, l1.longitude " +
            "FROM LOCATIONS l1 WHERE l1.node IN (:ids)", 
            nativeQuery=true)
    List<Location> findMeasureByIds(@Param("ids") String[] ids);
}
在这里,您可以看到我试图执行的查询,但它不起作用。我不知道是否可以在那里使用数组,或者参数必须是字符串和/或整数,我在任何地方都找不到

我尝试过几种组合,比如使用格式正确的简单字符串或长数组。。但到目前为止,一切都不起作用

提前谢谢

解决方案:


我向查询中添加了更多功能,因为我需要检索为每个节点标识符插入的最后一行。因此有MAX函数和内部连接来完成这项工作。

使用集合而不是数组集,并确保它不是空的,否则查询将无效

此外,没有理由为此使用本机查询,并且参数周围不应该有括号:

@Query("SELECT l1 FROM Location l1 WHERE l1.node.id IN :ids")
List<Location> findLocationsByNodeIds(@Param("ids") Set<String> ids);

非常感谢你!这就是我要找的。我使用Set是因为我的Node Java类将标识符声明为long。我已经编辑了这个问题以包含解决方案。我之所以使用nativeQuery,是因为我需要使用带有内部联接的agregation函数MAX,我不知道如果没有nativeQuery选项,是否可以这样做。再次感谢!
@Query("SELECT l1 FROM Location l1 WHERE l1.node.id IN :ids")
List<Location> findLocationsByNodeIds(@Param("ids") Set<String> ids);