Maksim Bezmen
Maksim Bezmen

Reputation: 88

How to batch update using jooq without id

create.batchUpdate(gameOrders
    .stream()
    .map(x -> {
      var game = new GameRecord();
      game.setOrder(x.newOrder());
      game.setId(x.id()); <------- this 
      // Prevent setting the ID to itself
      game.changed(GAME.ID, false);
      return game;
    })
    .toList())
  .executeAsync();

It is works fine. Because in this case I'm using id (PK)

create.batchUpdate(gameOrders
        .stream()
        .map(x -> {
          var game = new GameRecord();
          game.setOrder(x.newOrder());
          game.setName(x.newOrder());
          // Prevent setting the NAME to itself
          game.changed(GAME.NAME, false);
          return game;
        })
        .toList())
      .executeAsync();

I want to do it, but nothing won't update in DB, because in this case I don't use id only newOrder and newOrder.

How I can use this way for batchUpdate, if I don't have ID (PK)?

Upvotes: 0

Views: 452

Answers (1)

Lukas Eder
Lukas Eder

Reputation: 221145

You're trying to do an UPDATE based on an UpdatableRecord, which always performs its work based on the primary key. But apparently, that's not what you want to do. You want to run something like this:

UPDATE game
SET
  order = ?,
  name = ?
WHERE something = ?

So, why not just write an UPDATE statement instead, with jOOQ?

ctx.update(GAME)
   .set(GAME.ORDER, order)
   .set(GAME.NAME, name)
   .where(GAME.SOMETHING.eq(something))
   .execute();

Updates can be batched as well, see

You don't have to do this with UpdatableRecord API

Upvotes: 1

Related Questions