-1

Here I try to make call from database and combine into new mono from different mono and flux.

public Mono<ListMovieWithKomenDTO> fetchMovieAndKomen(Integer movieId){
            Mono<Movie> movie = findById(movieId).subscribeOn(Schedulers.elastic());
            Flux<MovieKomen> movieKomen = getKomenByMovieId(movieId).subscribeOn(Schedulers.elastic());
            return Mono.zip(movie, movieKomen.collectList(), movieMovieKomenDTOBiFunction);
        }
private BiFunction<Movie, List<MovieKomen>, ListMovieWithKomenDTO> movieMovieKomenDTOBiFunction = (x1, x2) -> ListMovieWithKomenDTO.builder()
                // .age(x1.getAge())
                .id(x1.getId())
                .name(x1.getName())
                .status(x1.getStatus())
                .detail(x1.getDetail())
                .url(x1.getUrl())
                .movieKomen(x2).build();

In here I make db call twice for header ( like movie ) and detail ( like movie comment ) to separate them. After I make retrieve two different data, I want to join into new mono data based on flux data and mono. to make them into one data, I make DTO to put together from movie table and comment table but it failed. I assume that errors from mono.zip to get data into one new mono.

Here the error from debug console

java.lang.IllegalArgumentException: Cannot encode parameter of type org.springframework.r2dbc.core.Parameter
    at io.r2dbc.postgresql.ExtendedQueryPostgresqlStatement.bind(ExtendedQueryPostgresqlStatement.java:89) ~[r2dbc-postgresql-0.8.10.RELEASE.jar:0.8.10.RELEASE]

Thank you

  • You can first remove both `subscribeOn` they are not needed, and if you needed them you only need one, because as stated in the docs, subscribeOn will affect the entire chain. Please read the reference that explains how and when to use `subscribeOn` – Toerktumlare Dec 20 '21 at 17:02
  • it still give error when I remove one of them. any suggestion sir ? thank you – Adam Abdullah Dec 20 '21 at 23:05

1 Answers1

0

Problem is in my repository I used

public interface MovieKomenRepository  extends ReactiveCrudRepository<MovieKomen,Integer> {
    @Query("select * from m_movie_komen where m_movie_id = $1")
    Flux<MovieKomen> findByMovieId(int movie_id);
}

in above example, I used $1 for the param in query. But when I change my code like bottom. It works like a charm.

public interface MovieKomenRepository  extends ReactiveCrudRepository<MovieKomen,Integer> {
    @Query("select * from m_movie_komen where m_movie_id = :movie")
    Flux<MovieKomen> findByMovieId(@Param("movie") int movie_id);
}

so if someone want to use my service code is fine but careful in repository. we should not used '$1' instead ':movie'. so the problem not in service or mono/flux. but in my repository

Thank you.