Best way to update existing POJO?

23 views
Skip to first unread message

BTJ

unread,
Mar 26, 2018, 5:46:58 AM3/26/18
to jOOQ User Group
I am back to JOOQ after I used JOOQ for as project some years ago but there is one thing that is not clear to me.
When fetching a POJO and updating this object, what is the best way to update the record in the database?
I see I have been using the dsl.executeUpdate method but I am trying to use ExecuteWithOptimisticLocking and I loose this feature when using this method.
So how should I move from POJO to records to use the store method? Or is it another way to do this?

Also, a bit on the sideline. If I need more functionality in my POJOs, what's the best way to "generate" POJOs? Just copy the field names from the record object and create my own POJOs or?


Regards,

BTJ

BTJ

unread,
Mar 26, 2018, 7:45:09 AM3/26/18
to jOOQ User Group
Ok, I came up with this code but not sure if this is the best way?

PersonRecord personRecord = dsl.newRecord(Tables.PERSON, person);
if (person.getId() != null) {
    personRecord
.changed(Tables.PERSON.ID, false);
    personRecord
.changed(Tables.PERSON.VERSION, false);
}
personRecord.store();

Lukas Eder

unread,
Mar 29, 2018, 9:58:06 AM3/29/18
to jooq...@googlegroups.com
Hello,

This question keeps popping up on the list and elsewhere, and I always have the same counter question. If you need optimistic locking, then why not just use the record all the way to help you keep track of the internal flags? What are you gaining from using the POJO intermediary? Sure, you can prevent the dependency on jOOQ where you might prefer not to have such a dependency, i.e. the UI.

But since you want optimistic locking to apply, you have an implicit dependency anyway, so why not just use the Record?

If using the POJO is imperative, then yes, your code is one reasonable way to transfer information back to the record.

Regarding your question about adding more logic to your POJOs, you can extend the JavaGenerator to generate "custom code sections" for you, in any generated artefact:

You can also completely override the generation of POJOs in JavaGenerator and generate whatever you need inside of them.
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.

BTJ

unread,
Mar 30, 2018, 11:07:28 AM3/30/18
to jOOQ User Group
Yes, I do not like to use generated classes dependent on other libraries in code that do not need to know about other libraries like JOOQ.. But it might just be me.. :)

But this helps yes, thx.. :)


BTJ
To unsubscribe from this group and stop receiving emails from it, send an email to jooq-user+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages