Search code examples
javasqljooq

INSERT don't returning multiple generated ids


I am currently trying to implement an insert (see https://www.jooq.org/doc/latest/manual/sql-building/sql-statements/insert-statement/insert-returning/). The difference is that I don't use values ​​directly, but the set with record.

In the log I see that 2 inserts are executed, but in the Records Result there is always only 1 result. Test is with H2.

FooRecord foo1;
FooRecord foo2;

   final Result<?> result =
            create.insertInto( FOO)
                  .set( foo1 )
                  .set( foo2 )
                  .returningResult( FOO.ID )
                  .fetch();

What am I doing wrong?


Solution

  • Check the example of the INSERT .. SET documentation, which lists a call to newRecord(). You have to place a marker between your records to clearly distinguish between them:

    final Result<?> result =
    create.insertInto( FOO)
          .set( foo1 )
          .newRecord() // This call is necessary
          .set( foo2 )
          .returningResult( FOO.ID )
          .fetch();
    

    Otherwise, the consecutive set() calls just override one another.