0

I work with R2DBC and i need to execute query, whiсh on request returns Flux of my entities and after that i need to convert this entities to DTO's,but to create an DTO i need to make another query to the database for each entity, which returns some special info from another tables, for example:

This code doesn't work when total number of Ids exceeds 512

 orderRepository.findByIds(listIds).flatMap{ order->
        eventRepostiry.findByOrderId(order.id).map{events->
           entityToDtoMapper.map(order,events,OrderWithEventsDto::class.java)
        }
    } 

concatMap doesn't help.

But this code works

orderRepository.findByIds(listIds).collectList().flatMapMany{orders->
        Flux.fromIterable(orders)
    }.flatMap{ order->{
          eventRepository.findByOrderId(order.id).collectList().flatMapMany{ events->
              Flux.fromIterable(events)
          }.map { event->
              entityToDtoMapper.map(order,events,OrderWithEventsDto::class.java)
          }
       }
    }

I think there’s a better solution to this problem. How am I supposed to do these queries right?

  • Could you elaborate on what you mean by "code doesn't work"? – Martin Tarjányi Mar 23 '20 at 18:26
  • @MartinTarjányi Issuing a query while not all results are fetched yet can exhaust the prefetch buffer of 512 items and cause my result stream to stuck, for more information see [link](https://stackoverflow.com/questions/60145595/how-to-join-tables-in-r2dbc) – kkaminsky Mar 24 '20 at 04:44

0 Answers0