What the best/correct way to update a pojo?

1,088 views
Skip to first unread message

BTJ

unread,
Sep 12, 2016, 7:27:50 AM9/12/16
to jOOQ User Group
Just started using JOOQ and so far I really like it but I don't think I have a complete picture of how to use it yet..

At the moment, a store method for me looks like this:

public boolean store(Pupil pupil)
    {
        PupilsRecord record;
        if (pupil.getId() == null)
        {
            record = create.newRecord(PUPILS, pupil);
        }
        else
        {
            record = create.fetchOne(PUPILS, PUPILS.ID.equal(pupil.getId()));
            record.setName(pupil.getName());
            etc...           
        }

        return record.store() == 1;
    }

Is there someway I can auto fill the record instead of manually updating every field in the record when updating?

Any other comments about how to store in JOOQ?


Regards,

BTJ

Lukas Eder

unread,
Sep 12, 2016, 1:44:11 PM9/12/16
to jooq...@googlegroups.com
Hi BTJ,

Thanks for your enquiry. What you're looking for is something like record.from(Object):

This is the inverse operation of Record.into(Class) or Record.into(Object).

Note that for historic reasons, when you modify (i.e. set) a Record's primary key value, the store() operation will execute an INSERT statement, as the historic assumption is that primary key values cannot be modified. If you want to use store(), you may need to reset the primary key's changed flag via Record.changed(ID, false):

Hope this helps.
Let me know if you have any additional questions and I'm very happy to help
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,
Sep 12, 2016, 5:32:23 PM9/12/16
to jOOQ User Group
Thx...

So if I use record.from, will the store method do an update as long as the id field is not null?
Also, are you saying the using the store method is not the best way to do this or?

BTJ
To unsubscribe from this group and stop receiving emails from it, send an email to jooq-user+...@googlegroups.com.

Lukas Eder

unread,
Sep 13, 2016, 1:41:08 AM9/13/16
to jooq...@googlegroups.com
2016-09-12 23:32 GMT+02:00 BTJ <bt.jo...@gmail.com>:
Thx...

So if I use record.from, will the store method do an update as long as the id field is not null?

By default, no, it will run an insert because record.from() will have modified the primary key column. Do note that by "modify" (i.e. Record.changed()), jOOQ understands that some client code called Record.set(ID, someValue), regardless of the value, i.e. regardless if the value has actually changed.
 
Also, are you saying the using the store method is not the best way to do this or?

Sure, it's a good solution, you just need to understand how it works, and how the jOOQ Record.changed() model works.

I hope this helps,
Lukas

BTJ

unread,
Sep 13, 2016, 2:24:49 AM9/13/16
to jOOQ User Group
Ok but since I have to separate by insert or update anyway then perhaps it's just as easy do call executeUpdate or executeInsert instead... :)

BTJ

Lukas Eder

unread,
Sep 13, 2016, 6:01:29 AM9/13/16
to jooq...@googlegroups.com
2016-09-13 8:24 GMT+02:00 BTJ <bt.jo...@gmail.com>:

Ok but since I have to separate by insert or update anyway then perhaps it's just as easy do call executeUpdate or executeInsert instead... :)

Sure, those are just different APIs for Record.update() and Record.insert(). Note that Record.store() is "just" convenience to make the update() or insert() decision based on whether the primary key value of a record is available and unchanged.

Lukas

BTJ

unread,
Sep 13, 2016, 6:24:46 AM9/13/16
to jOOQ User Group
That's what I thought... Thx.. :)

BTJ 
Reply all
Reply to author
Forward
0 new messages