javaspring-webfluxspring-data-r2dbcr2dbcr2dbc-postgresql

Insert Query using @Query in R2DBCRepository SpringWebFlux not Working


Seems Composite Key/@Embedded are not supported in R2DBC - link , I am trying to insert data using @Query annotation like below.

      @Repository("testRepository")
      public interface TestRepository extends R2dbcRepository<Test, testEmbeddedId> {
    
      @Modifying
      @Query(value = "insert into test(id, name, status, created_date) VALUES ('b128f97d-d52c-4677-8746-00e2959c9ec6', 'c', 'd', '2022-10-28')")
      @Transactional
      void insertData();
    }

But this is failed without giving any error. Any idea about this issue or any alternative approach here.


Solution

  • I think that the signature of your method insertData should return a Mono<Test> on which you should subscribe to execute the insert statement, i.e. declare method insertData as

    @Modifying
    @Query(value = "insert into test(id, name, status, created_date) VALUES ('b128f97d-d52c-4677-8746-00e2959c9ec6', 'c', 'd', '2022-10-28')")
    @Transactional
    Mono<Test> insertData();
    

    and then execute it as

    insertData().subscribe();
    

    This worked for me with a simple primary key.