java - 带有可迭代的Spring数据findAllBy返回空数组
问题描述
使用 a和自定义方法返回具有匹配属性的所有对象是在调用ReactiveMongoRepository
以外的任何对象上返回一个空集合。findAllById
我想知道我是否在这里误解了某些东西,而这只适用于 ID 字段或其他东西?
我正在使用的界面:
@Repository
public interface VideoRepository extends ReactiveMongoRepository<Video, String> {
Flux<Video> findAllByHash(Iterable<Long> hash);
}
我只是通过以下方式调用它:
@GetMapping("duplicates")
public Flux<Video> duplicates() {
// {...}
List<Long> hashList = duplicateDTOs
.stream()
.map(duplicateDTO -> Long.valueOf(duplicateDTO.getHash()))
.collect(Collectors.toList());
return videoRepository.findAllByHash(hashList);
}
作为参考,有问题的 POJO:
@Data
@Builder
@Document
@AllArgsConstructor
@NoArgsConstructor
public class Video {
@Id
String id;
long hash;
//{...}
}
我已经确认我正在传递三个值,它们与POJO上设置hashList
的自定义hash
属性相匹配。Video
这是否不应该返回所有Video
具有匹配自定义hash
属性的对象,就像我对属性做同样的事情时所做的那样id
?
解决方案
findAllByHashIn(Collection<Long> hashes);
我以前从未使用过Iterable
作为自定义 JPA 存储库方法的参数,但我会将名称翻译findAllByHash
为“获取单个哈希值并查找拥有该值的所有条目”,并且签名将是findAllByHash(Long hash)
.
您的动机有点不同:您希望在搜索过程中使用所有哈希值。根据这张表,
Keyword | Sample | JPQL snippet
In | findByAgeIn(Collection<Age> ages) | … where x.age in ?1
Spring JPA 支持逻辑IN
并接受 的子类Collection
,因此它可以是
findAllByHashIn(Collection<Long> hashes);
findAllByHashIn(List<Long> hashes);
更新
出于好奇,我写了自己的Iterable
,并不是Collection
看到方法失败。不出所料,Spring抛出
IllegalArgumentException:参数值 XXX 与预期类型不匹配 [java.lang.Long (n/a)]。
虽然它需要一个Long
参数,但它运行良好Collection
(我用过Arrays.asList(1L, 2L)
),但执行一个愚蠢的 SQL 查询:
... from XXX XXX0_ where XXX0_.hash=(? , ?)
binding parameter [1] as [BIGINT] - [1]
binding parameter [2] as [BIGINT] - [2]
添加了findAllByHashIn
,IN
并且查询看起来不错:
... from XXX XXX0_ where XXX.hash in (? , ?)
binding parameter [1] as [BIGINT] - [1]
binding parameter [2] as [BIGINT] - [2]
推荐阅读
- angular - Ngxs @Select 装饰器可以返回 BehaviorSubject 而不是 Observable 吗?
- reactjs - 如何在嵌套函数中设置无状态函数中的状态?
- html - 我无法重现模型渐变
- ios - 如何访问我的应用程序的派生数据文件夹本身?
- r - 在 R 中排除具有模式匹配的行
- angular - 您如何在 ag-grid 模块中使用多个组件?
- c - 另一个 ListView 中的 ListView 没有组装正确的模板
- python - 没有弃用警告的 unicode_escape
- javascript - Chart.js - 如何更新数据
- python - Python:如何制作非阻塞while循环?