Jooq CRUD / UpdatableRecord right use for upsert of newRecord()

63 views
Skip to first unread message

Denis Miorandi

unread,
Oct 22, 2016, 10:05:10 AM10/22/16
to jOOQ User Group
I'm trying to use UpdatableRecord store() method, but it's not clear to me the right way.
In my scenario I would like 
  • create a record in memory, so 
  • store record wheter exists or not so )
rec=ctx.newRecord(MYTABLE)
rec.setField1(1)
rec.setField2(1)
....
rec.store()

Issue is store() try to always insert it (wheter exists or not on db) cause not loaded from db with rec.refresh(),
so that I've tried to do same thing using a refresh on rec (after set keys before set other values) but refresh()
fail is record doesn't exists. So:

  • refresh() works only on existings records
  • store() works only if a record is loaded from refresh() otherwise always insert
Am I right? Am I missunderstanding something?
How can I store a new record wheter exists or not on db?

Tks
Denis


Lukas Eder

unread,
Oct 25, 2016, 5:25:19 PM10/25/16
to jooq...@googlegroups.com
Hi Denis,

UpdatableRecord currently doesn't have an "UPSERT" feature (or INSERT ON DUPLICATE KEY UPDATE, etc.) This is a pending feature request:

I guess we've postponed it long enough now, it shouldn't be too hard to implement...
In the meantime, you can pass your record to an actual INSERT ON DUPLICATE KEY UPDATE statement:

ctx.insertInto(MYTABLE)
   .set(rec)
   .onDuplicateKeyUpdate()
   .set(rec) // Maybe manually reset the PK columns again, here...
   .execute();

Hope this helps,
Lukas

--
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 jooq-user+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Denis Miorandi

unread,
Oct 26, 2016, 2:48:50 AM10/26/16
to jooq...@googlegroups.com
Tks Lukas this is exactly what I'm doing at now.

--
You received this message because you are subscribed to a topic in the Google Groups "jOOQ User Group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jooq-user/NRjbXIrPEfc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jooq-user+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Dott. Denis Miorandi
via dei Vignai 27
38060 - Nogaredo (TN)



Lukas Eder

unread,
Apr 6, 2020, 10:14:57 AM4/6/20
to jOOQ User Group
jOOQ 3.14 will support UpdatableRecord.merge() for this purpose: https://github.com/jOOQ/jOOQ/issues/2961
Reply all
Reply to author
Forward
0 new messages