Skip to content

Batch insert via fluent API & returning generated keys... #2564

Answered by hgschmie
jcubeta asked this question in Q&A
Discussion options

You must be logged in to vote

So this works fine with Postgres:

@Test
    void testBatchInsertReturningIds() {
        List<String> names = Arrays.asList(
            "Brian",
            "Steven",
            "Matthew",
            "Artem",
            "Marnick",
            "Henning");


        PreparedBatch batch = handle.prepareBatch("insert into something (name) values (?) returning id");
        names.forEach(batch::add);

        BatchResultBearing batchResult = batch.executePreparedBatch("id");
        List<Integer> ids = batchResult.mapTo(Integer.class).list();

        assertThat(ids).hasSize(6);
        assertThat(ids).containsExactly(1, 2, 3, 4, 5, 6);
    }

The challenge is that returning data from the d…

Replies: 2 comments 3 replies

Comment options

You must be logged in to vote
2 replies
@jcubeta
Comment options

@jcubeta
Comment options

Answer selected by jcubeta
Comment options

You must be logged in to vote
1 reply
@jcubeta
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants