title: Retrieve generated IDs when doing bulk insert using Spring Data R2DBC
body: <p>I have a scenario where my table has an autogenerated id column and <strong>I need to bulk insert items into db and fetch the generated ids</strong>. Is there any way I can achieve that?</p>
  <p>This is my table:</p>
  <pre><code>CREATE TABLE test_table (
    `id` SERIAL NOT NULL,
    `name` VARCHAR(100) NOT NULL,
    `created_date` DATETIME NOT NULL,
    PRIMARY KEY (`id`)
  );
  </code></pre>
  <p>To save a list of items into this table, the code I am using:</p>
  <pre><code>String initialSql = &quot;INSERT INTO test_table(`name`,`created_date`) VALUES &quot;;
  
      List&lt;String&gt; values =
          dummyEntities.stream()
              .map(dummyEntity -&gt; &quot;('&quot; + dummyEntity.getName() + &quot;','&quot;
                  + dummyEntity.getCreatedDate().atZoneSameInstant(ZoneId.of(&quot;UTC&quot;)).toLocalDateTime().toString() + &quot;')&quot;)
              .collect(Collectors.toList());
  
      String sqlToExecute =  initialSql + String.join(&quot;,&quot;, values);
      client.execute(sqlToExecute)
               .//Then what?
  </code></pre>
  <p>The generated SQL statement (from DEBUG Logs):</p>
  <pre><code>2020-09-15 18:59:32.613 DEBUG 44801 --- [actor-tcp-nio-1] o.s.d.r2dbc.core.DefaultDatabaseClient   : Executing SQL statement [INSERT INTO test_table(`name`,`created_date`) VALUES ('Abhi57','2020-09-15T13:29:29.951964'),('Abhi92','2020-09-15T13:29:29.952023')]
  </code></pre>
  <p>I even tried using <code>ConnectionFactory</code>, still no clue</p>
  <pre><code>    Mono.from(connectionFactory.create())
          .map(Connection::createBatch)
          .map(batch -&gt; {
            dummyEntities.forEach(dummyEntity -&gt; {
              String sql = String.format(&quot;INSERT INTO `test_table` (`name`,`created_date`) VALUES ('%s','%s');&quot;, dummyEntity.getName(),
                  dummyEntity.getCreatedDate().atZoneSameInstant(ZoneId.of(&quot;UTC&quot;)).toLocalDateTime().toString());
              batch.add(sql);
            });
            return batch;
          })
          .flatMap(batch -&gt; Mono.from(batch.execute()))
          .//Then what?
  </code></pre>
  <p>For reference, <code>dummyEntities</code> variable holds a list of <code>DummyEntity</code> objects. And the <code>DummyEntity</code> class looks like this:</p>
  <pre><code>@Table(&quot;test_table&quot;)
  public class DummyEntity implements Persistable&lt;Long&gt; {
  
    @Id
    @Column(&quot;id&quot;)
    private Long id;
  
    @Column(&quot;name&quot;)
    private String name;
  
    @Column(&quot;created_date&quot;)
    private OffsetDateTime createdDate;
  
    //Getter,Setter
    @Override
    public boolean isNew() {
      return id == null;
    }
  }
  </code></pre>
  <p>Dependencies used: 2.3.2.RELEASE</p>
  <pre><code>    implementation 'org.springframework.boot:spring-boot-starter-webflux'
      implementation 'org.springframework.boot:spring-boot-starter-data-r2dbc'
      implementation 'dev.miku:r2dbc-mysql:0.8.2.RELEASE'
  </code></pre>