Hi all,
I'm trying to (batch) insert a set of records and have their primary
keys set afterwards. I have tried with both 3.6.4 and 3.9.1. We use
MySQL.
I started out with
@SuppressWarnings({ "unchecked", "rawtypes" })
final Class<UpdatableRecord<?>> updatableRecordClass = (Class)
UpdatableRecord.class;
DSL
.using(configuration)
.batchInsert(records.toJavaArray(updatableRecordClass))
.execute();
(in the #run of a TransactionalRunnable). This works fine, i.e. the
records show up in the database, but my UpdatableRecords still have a
"null" primary key.
After some more digging I found out about DSLContext#insertInto and
InsertReturningStep#returning. So I tried this:
for (final UpdatableRecord<?> record : records) {
final InsertSetStep<?> iss = DSL
.using(configuration)
.insertInto(record.getTable());
final List<Field<?>> changedFields = Lists.newArrayList();
final List<Object> values = Lists.newArrayList();
for (final Field<?> field : record.fields()) {
if (record.changed(field)) {
changedFields.add(field);
values.add(record.getValue(field));
}
}
iss
.columns(changedFields)
.values(values)
.returning(record.getTable().getPrimaryKey().getFields())
.execute();
}
Again, the records show up fine in the database but my
UpdatableRecords still do not have a primary key.
Finally, I noticed that InsertReturningStep uses #fetchOne so I tried
that instead of #execute but it returns null.
I am at a loss as to how to make this work. How do I get the primary
key of an UpdatableRecord I just inserted into said UpdatableRecord?
Cheers,
Hilco
--
You received this message because you are subscribed to the Google Groups "jOOQ
User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.