使用 ReactiveMongoRepository
和自定义方法返回具有 match 属性的所有对象,将在除 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;
//{...}
}
我已经确认我在hashList
中传递了三个值,这些值与Video
POJO 上设置的自定义hash
属性相匹配。
这不应该返回所有具有匹配的自定义hash
属性的Video
对象,就像我做同样的事情但对于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
。不出所料,春天扔了
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]