4

I'm new to Project Reactor and R2DBC. How to receive and merge Flux<Child> with Mono<Parent> properly using Spring Data R2DBC reactive repositories?

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

}

My solution is:

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 );
        });      
}
Askolein
  • 3,250
  • 3
  • 28
  • 40

1 Answers1

3

Assuming the existence of a withChildren(Flux<Child> children) type method, you can just do:

parentRepository.findById(parentId)
    .map(p -> p.withChildren(childRepository.findAllByParentId(parentId)));

However, this is a bit odd - you wouldn't usually have a Flux on a DAO like that as you'd need to subscribe to it and manage the content separately. You'd more normally have a List<Child> instead. For that situation, you can collect the child stream as a list, zip() the corresponding Mono publishers together, then combine them into your final Parent object.

So assuming a withChildren(List<Child> children) method:

Mono.zip(parentRepository.findById(parentId),
    childRepository.findAllByParentId(parentId).collectList(),
    (t1,t2) -> t1.withChildren(t2));
Michael Berry
  • 70,193
  • 21
  • 157
  • 216
  • Thanks for answer. If just copy `Flux`, then `childRepository.findAllByParentId(parentId)` is not executed, because I need to subscribe to this `Flux`. How to do this using Project Reactor operators? – Kiryl Valiushka Jan 22 '20 at 08:25
  • But I want to make application fully reactive, then I need to use `Flux` instead of using collections, right? – Kiryl Valiushka Jan 22 '20 at 09:47
  • 2
    @KirillValyushko No, that makes no sense. `Flux` is a stream of data, it's not a replacement for using collections. Being fully reactive is about being non-blocking (and `collectList()` is non-blocking, since it provides a `Mono`). You may wish to do some reading around reactor and reactive programming in general, otherwise you're likely to come increasingly unstuck. – Michael Berry Jan 24 '20 at 22:14
  • 3
    @MichaelBerry how can we achieve this in case when `parentRepository` return `Flux` ? execute __N__ times `childRepository.findAllByParentId(parentId)` for each Parent object found in database ? – Halayem Anis Jun 28 '20 at 09:19
  • @MichaelBerry and how should it look if I want to retrieve all `parent` elements with children without calling `findAllByParentId` for each parent? Is it possible? edit: sorry, I haven't notice that @HalayemAnis asked the same question.. but maybe worth pinging you again :) – Konrad Drozd Feb 25 '22 at 17:16
  • In regards with the non-blocking nature of r2dbc, the infamous N+1 problem is related in any way with using for each item 2 trips to the database here? – Stefan Jan 12 '23 at 21:49