我是 Project Reactor 和 R2DBC 的新手。如何正确使用 Spring Data R2DBC 响应式存储库接收和Flux<Child>
合并?Mono<Parent>
家长:
@Table("parent")
public class Parent{
@Id
private Long id;
private String name;
@Transient
private Flux<Child> children;
父存储库:
interface ParentRepository extends ReactiveCrudRepository<Parent, Long> {
@Query()
Mono<Parent> findOneByName(String name);
孩子:
@Table("child")
public class Child{
@Id
private Long id;
子库:
interface ChildRepository extends ReactiveCrudRepository<Child, Long> {
@Query()
Flux<Child> findAllByParentId(Long parentId);
ParentPersistenceAdapter:
public Mono<Parent> findParent(String parentName) {
return parentRepository.findOneByName(parentName)
//how to invoke childRepository.findAllByParentId()
//and set this Flux to Parent's Mono
}
我的解决方案是:
public Mono<Parent> findParent(String parentName) {
return parentRepository.findOneByName(parentName)
.map(parent -> {
Flux<Child> children = childRepository.findAllByParentId(parent.getId())
children.subscribe();
return parent.setChildren(children );
});
}