简体   繁体   English

具有可迭代的 Spring 数据 findAllBy 返回空数组

[英]Spring data findAllBy with iterable returns empty array

Using a ReactiveMongoRepository and a custom method to return all objects with the matching property is returning a empty collection on anything other than the findAllById call.使用ReactiveMongoRepository和自定义方法返回具有匹配属性的所有对象会返回一个空集合,除了findAllById调用。

I'm wondering if I just misunderstood something here and this only works on the ID field or something?我想知道我是否只是在这里误解了一些东西并且这只适用于 ID 字段或其他什么?

The interface I'm using:我使用的界面:

@Repository
public interface VideoRepository extends ReactiveMongoRepository<Video, String> {
    Flux<Video> findAllByHash(Iterable<Long> hash);
}

And I'm just calling that via:我只是通过以下方式调用它:

@GetMapping("duplicates")
public Flux<Video> duplicates() {
    // {...}

    List<Long> hashList = duplicateDTOs
            .stream()
            .map(duplicateDTO -> Long.valueOf(duplicateDTO.getHash()))
            .collect(Collectors.toList());

    return videoRepository.findAllByHash(hashList);
}

For reference, the POJO in question:作为参考,有问题的 POJO:

@Data
@Builder
@Document
@AllArgsConstructor
@NoArgsConstructor
public class Video {

    @Id
    String id;

    long hash;

    //{...}
}

I have confirmed I'm passing in three values in the hashList which match the custom hash property set on the Video POJO.我已经确认我在hashList传递了三个值,它们与Video POJO 上设置的自定义hash属性相匹配。

Should this not return all Video objects which have the matching custom hash property as it does when I do the same thing but for the id property?这不应该返回所有具有匹配自定义hash属性的Video对象,就像我做同样的事情但对于id属性时所做的那样吗?

findAllByHashIn(Collection<Long> hashes);

I've never used an Iterable as a parameter to a custom JPA repository method before, but I would translate the name findAllByHash as "take a single hash value and find all entries possessing that value" and the signature would be findAllByHash(Long hash) .我以前从未使用Iterable作为自定义 JPA 存储库方法的参数,但我会将名称findAllByHash为“采用单个哈希值并找到具有该值的所有条目”,并且签名将是findAllByHash(Long hash) .

Your motivation is a bit different: you want all hashes to be used during the search.您的动机有点不同:您希望在搜索过程中使用所有散列 According to this table ,根据这张表

Keyword | Sample                             | JPQL snippet

In      | findByAgeIn(Collection<Age> ages)  | … where x.age in ?1

Spring JPA support logical IN and accepts a subclass of Collection , so it could be Spring JPA 支持逻辑IN并接受Collection的子类,因此它可以是

findAllByHashIn(Collection<Long> hashes);
findAllByHashIn(List<Long> hashes);

Update更新

Out of curiosity, I wrote own Iterable which isn't a Collection to see the method fail.出于好奇,我编写了自己的Iterable ,它不是一个Collection来查看方法失败。 As expected, Spring threw不出所料,Spring抛出了

IllegalArgumentException: Parameter value XXX did not match expected type [java.lang.Long (n/a)]. IllegalArgumentException:参数值 XXX 与预期类型 [java.lang.Long (n/a)] 不匹配。

Although it expects a Long parameter, it runs well with a Collection (I used Arrays.asList(1L, 2L) ), but executes a silly SQL query:尽管它需要一个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]

With findAllByHashIn , IN is added and the query looks nice:使用findAllByHashIn ,添加了IN并且查询看起来不错:

... from XXX XXX0_ where XXX.hash in (? , ?)
binding parameter [1] as [BIGINT] - [1]
binding parameter [2] as [BIGINT] - [2]

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM