I see you're using the constructor of the Record type directly in that
bit of sample code -- in the code referenced in your original post, are
you also doing that? Have you tried using `DSLContext#newRecord(<table
type>)` to create the record so that it's bound to the underlying context?
On 08/31/2018 06:37 AM, Marcus Gattinger wrote:
Thanks for your response. As I mentioned I've compared the use case with
Hibernate. Both libraries (i. e. Hibernate and jOOQ) use the same JDBC
driver and execute INSERT INTO table (field1, field2, ...) VALUES
(value1, value2, ...).
However Hibernate does not execute an additional SELECT statement to get
the generated key value.
I tried your suggested solution (sligtly modified) to insert the row and
as far as I can see, I get the generated key value without an additional
SELECT statement.
|
DSLContextdatabase =applicationContext().getBean(DSLContext.class);
ClientRecordrecord =newClientRecord();
// insert
record.setName(randomUUID().toString());
Recordinserted
=database.insertInto(record.getTable()).set(record).returning(record.key().fields()).fetchOne();
record.setId((Integer)inserted.getValue(0)).changed(false);
System.out.println(record.getId());
// update
record.setName(randomUUID().toString());
record.attach(database.configuration());
record.store();
|
However it would be preferable if jOOQ would not require me to set the
id in the inserted record manually and mark the field as unchanged to
make the update working.
Regards,
Marcus
Am Donnerstag, 30. August 2018 20:25:31 UTC+2 schrieb Samir Faci:
I think that's a limitation of the query you're running. If you
want to do all this in one operation you may want to look at using
upserts.
https://dev.mysql.com/doc/refman/8.0/en/insert-on-duplicate.html
<https://dev.mysql.com/doc/refman/8.0/en/insert-on-duplicate.html>
I'm not sure if your version of MySQL supports this but otherwise
you are forced to do a select otherwise.
I'm not 100% sure on the MySQL syntax jooq pattern to use, but
something like this should work.
|Record result = dslContext.insertInto(record.getTable())
.set(record) .onDuplicateKeyUpdate() .set(record)
.returning(record.field1()) .fetchOne(); long id = result.getId(); |
On Thu, Aug 30, 2018 at 5:12 AM Marcus Gattinger <[email protected]
<javascript:>> wrote:
BTW: Im using Record.store() to insert new rows to the table.
--
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] <javascript:>.
For more options, visit https://groups.google.com/d/optout
<https://groups.google.com/d/optout>.
--
Thank you
Samir Faci
https://keybase.io/csgeek
--
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]
<mailto:[email protected]>.
For more options, visit https://groups.google.com/d/optout.
--
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.