Raf*_*ado 6 postgresql spring spring-data-jpa
是否可以在Spring Repository @Query注释中使用Array对象作为参数?
我正在尝试检索表中列节点存在于String数组中的所有行.是否可以使用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;
}
Run Code Online (Sandbox Code Playgroud)
其中节点引用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);
}
Run Code Online (Sandbox Code Playgroud)
在那里你可以看到我正在尝试执行的查询,但它无法正常工作.我不知道是否可以在那里使用数组,或者参数必须只是字符串和/或整数,我无法在任何地方找到它.
我已经尝试了几种组合,例如使用具有正确格式的简单字符串或长数组......但到目前为止还没有任何工作.
提前致谢.
解:
@Query(value="SELECT * FROM LOCATIONS l1 " +
"INNER JOIN (SELECT node, MAX(id) AS id FROM LOCATIONS GROUP BY node) l2 " +
"ON l1.node = l2.node AND l1.id = l2.id " +
"WHERE l1.node IN :ids", nativeQuery=true)
List<Location> findLastLocationByIds(@Param("ids") Set<Long> ids);
Run Code Online (Sandbox Code Playgroud)
我已经为查询添加了更多功能,因为我需要检索为每个节点标识符插入的最后一行.因此有MAX功能和INNER JOIN来完成这项工作.
JB *_*zet 11
使用集合而不是数组(Set<String>
),并确保它不为空(否则查询将无效.
此外,没有理由使用本机查询,并且您不应该在参数周围使用括号:
@Query("SELECT l1 FROM Location l1 WHERE l1.node.id IN :ids")
List<Location> findLocationsByNodeIds(@Param("ids") Set<String> ids);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
11619 次 |
最近记录: |