DSLContext.batchStore method and removed records

43 views
Skip to first unread message

Estevão Góes

unread,
Apr 15, 2014, 1:26:28 PM4/15/14
to jooq...@googlegroups.com
Hi all, 
I recently discovered the DSLContext.batchStore method that can save or update records from a list direct to database. This is fabulous. Great code.
But I'm just wondering how I can handle removed records from list. Should I maintain a complementary list of removed records, or did I code something wrong? 

This is my code:

List<Person> people = DSLContext.fetch(Tables.Person);
people.remove(people.size() - 1);  //it removes last person 
DSLContext.batchStore(people).execute(); //it doesn't work, the record is not deleted.


Lukas Eder

unread,
Apr 16, 2014, 4:39:33 AM4/16/14
to jooq...@googlegroups.com
Hello Estevão,

Thanks for your suggestion. I'm afraid that you will currently need to maintain two separate lists, e.g.

List<People> people = ...
List<People> removed = new ArrayList<>();
removed.add(people.remove(people.size() - 1));

ctx.batchStore(people).execute();
ctx.batchDelete(removed).execute();

I wonder if a batchMerge() could be implemented. The question, however, would be how to identify those records that should be removed...?

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

Estevão Freitas

unread,
Apr 16, 2014, 10:09:20 AM4/16/14
to jooq...@googlegroups.com
In Hibernate and JPA we can use a flag called "deleteOrphans", so when those frameworks load collections, we use this flag to delete or not removed elements from collections.

I tried to code something like that, but I can't identity when a list element was retrieved from database or was just created.

Sent with MailTrack

Estevão de Freitas Góes
------------------------------------
Analista de Sistemas
Bel. Ciências da Computação (UFMA 2010)

Lukas Eder

unread,
Apr 16, 2014, 10:24:43 AM4/16/14
to jooq...@googlegroups.com
2014-04-16 16:09 GMT+02:00 Estevão Freitas <estevao...@gmail.com>:
In Hibernate and JPA we can use a flag called "deleteOrphans", so when those frameworks load collections, we use this flag to delete or not removed elements from collections.

I tried to code something like that, but I can't identity when a list element was retrieved from database or was just created.

Yes, for such a thing to work, a second-level cache would be useful, of course. We expressly want to avoid implementing such a cache as Hibernate/JPA already do a pretty good job when it comes to object-graph persistence.

Estevão Freitas

unread,
Apr 18, 2014, 12:24:01 PM4/18/14
to jooq...@googlegroups.com

But how can I know when a record is new or restored from database?

--

Lukas Eder

unread,
Apr 19, 2014, 2:57:42 AM4/19/14
to jooq...@googlegroups.com
One way to "know" this is to see if your primary key value is null or if it is set. This works well if you're using an IDENTITY / AUTO_INCREMENT column, as jOOQ will fetch that value from the database after store().

Note that you might not need to know this if you're calling UpdatableRecord.store().

Does that answer your question?

Happy Easter,
Lukas
Reply all
Reply to author
Forward
0 new messages