Skip to content
Advertisement

Obtaining a nested objects using Spring Data R2DBC

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 );
        });      
}

Advertisement

Answer

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));
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement