Spring响应式:链接存储库结果

问题描述 投票:0回答:1
Repository repo
Repository otherRepo

foreach entity : repo.FindAll() {
    entityFind = otherRepo.FindById(entity.Prop)
    if (entityFind != null) {
        return entityFind 
    }
}

如何使用弹簧反应器来做到这一点?

我可以使用blockFirst()在otherRepo中进行搜索,但它会中断反应链

我也尝试过使用handle()来控制流程,但是找到项目时我不会中断流程

有什么想法吗?谢谢

spring project-reactor reactive
1个回答
0
投票
如果您有这样的存储库,则对于repo1的每个记录,如果您需要从repo2中查找一条记录,则可能可以使用spring数据JPQL联接表并使用自定义方法,因为当前的方法可能会对性能产生影响。

您似乎只对第一张唱片感兴趣,只是给您一个想法,我们可以实现这样的目标。

return Flux.fromIterable(repo.findAll()) //assuming it returns a list .map(entity -> otherRepo.findById(entity.property)) // for each entity we query the other repo .filter(Objects::nonNull) // replace it with Optional::isPresent if it is optional .next(); //converts the flux to mono with the first record

© www.soinside.com 2019 - 2024. All rights reserved.