Я новичок в Project Reactor и R2DB C. Как правильно получать и объединять Flux<Child>
с Mono<Parent>
, используя Spring Data R2DB C реактивные репозитории?
Parent:
@Table("parent")
public class Parent{
@Id
private Long id;
private String name;
@Transient
private Flux<Child> children;
ParentRepository:
interface ParentRepository extends ReactiveCrudRepository<Parent, Long> {
@Query()
Mono<Parent> findOneByName(String name);
Child:
@Table("child")
public class Child{
@Id
private Long id;
ChildRepository:
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 );
});
}