You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-java@ibatis.apache.org by Nicholoz Koka Kiknadze <ki...@gmail.com> on 2008/05/29 23:44:06 UTC

Inserting lists

/* I apologize for re-posting this, but I think my previous post ended in
junk folders because I was using all-numeric mailbox */

Recently I found that I'm writing loops to insert lists of objects way too
often. I think it makes sense implementing methods like:

insertList("insertSingleSomeObject",List<SomeObject>)    (with
"insertSingleSomeObject" beeing sqlmap entry for inserting single object,
which I have to use in loops)
batchInsert("insertSingleSomeObject",List<SomeObject>)  (with batch size
configurable like row prefetch is)

updateList("updateSingleSomeObject",List<SomeObject>)
batchUpdate("updateSingleSomeObject",List<SomeObject>)


Think it will make iBatis  more consistent: we have queryForList, i.e.
getting list of objects in a single line of code, so it's quite natural to
expect that one should be able to persist the list in a single call.

Wonder what others think about it.

Nicholoz Koka Kiknadze

Re: Inserting lists

Posted by Nicholoz Koka Kiknadze <ki...@gmail.com>.
Thanks for an opinion Richard

Typically, there isn't a need to insert several items into the same table at
> once


How about shopping cart - most typical application ;) Actually I have
applicants with lists of faculties and exams. Every list property of newly
created object needs to be inserted, and I receive whole object from web
UI.  As for guarding from duplicates, I do it in my validation code,
database constraint is there only as last resort - protection from bugs.

Re: Inserting lists

Posted by Richard Yee <ry...@cruzio.com>.
Nicholoz,
I don't think it is a very common use case to insert a list in a 
database. QueryForList returns rows from a select query.  Typically, 
there isn't a need to insert several items into the same table at once. 
What would you do if you have duplicate items? Would you roll back, 
stop, or continue?

-Richard

Nicholoz Koka Kiknadze wrote:
> /* I apologize for re-posting this, but I think my previous post ended 
> in junk folders because I was using all-numeric mailbox */
>
> Recently I found that I'm writing loops to insert lists of objects way 
> too often. I think it makes sense implementing methods like:
>
> insertList("insertSingleSomeObject",List<SomeObject>)    (with 
> "insertSingleSomeObject" beeing sqlmap entry for inserting single 
> object, which I have to use in loops)
> batchInsert("insertSingleSomeObject",List<SomeObject>)  (with batch 
> size configurable like row prefetch is)
>
> updateList("updateSingleSomeObject",List<SomeObject>)
> batchUpdate("updateSingleSomeObject",List<SomeObject>)
>
>
> Think it will make iBatis  more consistent: we have queryForList, i.e. 
> getting list of objects in a single line of code, so it's quite 
> natural to expect that one should be able to persist the list in a 
> single call.
>
> Wonder what others think about it.
>
> Nicholoz Koka Kiknadze


Re: Inserting lists

Posted by Nicholoz Koka Kiknadze <ki...@gmail.com>.
Jeff,
Just for an argument ;)  -- you are understating powers of iBatis ;) iBatis
can return list of objects running multiple susbselects for each row
(<property column= select= /> ) - something you'd have to do manually in
pure JDBC. In fact, I'd go further allowing inserting object with list
properties in a single sqlmap method using somethimg similar to subselects,
but thats another story.

I understand you philosophy, but I'm trying to look from practical point of
view - the less boilerplate code in my app, the happier I am, quite a
selfish approach ;)

Re: Inserting lists

Posted by Jeff Butler <je...@gmail.com>.
What I meant is that the SQL select statement can return more than one
record by design.  The SQL insert statement works with only one
record.  In that sense, iBATIS is matching what SQL and JDBC do.

Jeff Butler


On Fri, May 30, 2008 at 7:53 AM, Nicholoz Koka Kiknadze
<ki...@gmail.com> wrote:
>> It is a trivial matter to write these methods
> Exactly Jeff,  that's why I hoped it would be a trivial addition to sqlmap
> API.
> I'd be happier not to write those trivial things over an over but rather
> keep them in one place. As for transactions, obviously one can revert back
> to looping in application if he wants to commit after single insert...
>
>> iBATIS wraps JDBC and SQL
> Not exatly, you do loop executing queryForList, freeing developers from
> writing those loops in app.
>
> But, OK, I can extend SqlMap to add those methods for my covenience ;)
>
> Thanks for opinion
>
>
>
>
>
>
>
>
>

Re: Inserting lists

Posted by Nicholoz Koka Kiknadze <ki...@gmail.com>.
> It is a trivial matter to write these methods
Exactly Jeff,  that's why I hoped it would be a trivial addition to sqlmap
API.
I'd be happier not to write those trivial things over an over but rather
keep them in one place. As for transactions, obviously one can revert back
to looping in application if he wants to commit after single insert...

> iBATIS wraps JDBC and SQL
Not exatly, you do loop executing queryForList, freeing developers from
writing those loops in app.

But, OK, I can extend SqlMap to add those methods for my covenience ;)

Thanks for opinion

Re: Inserting lists

Posted by Jeff Butler <je...@gmail.com>.
It is a trivial matter to write these methods yourself - you could
even write a utility method one time and be done with it:

insertList(String statementId, List<Object> records) {
  for (Object record : records) {
    sqlMap.insert(statementId, record);
  }
}

The question is - how do you want to handle transactions?  Is it all
one transaction, or is it individual transactions?  This is a user
decision - not a decision iBATIS should make for you.

So, my opinion is that we should not do this in iBATIS.  iBATIS wraps
JDBC and SQL, this would be more of an application level thing.

Jeff Butler


On Thu, May 29, 2008 at 4:44 PM, Nicholoz Koka Kiknadze
<ki...@gmail.com> wrote:
> /* I apologize for re-posting this, but I think my previous post ended in
> junk folders because I was using all-numeric mailbox */
>
> Recently I found that I'm writing loops to insert lists of objects way too
> often. I think it makes sense implementing methods like:
>
> insertList("insertSingleSomeObject",List<SomeObject>)    (with
> "insertSingleSomeObject" beeing sqlmap entry for inserting single object,
> which I have to use in loops)
> batchInsert("insertSingleSomeObject",List<SomeObject>)  (with batch size
> configurable like row prefetch is)
>
> updateList("updateSingleSomeObject",List<SomeObject>)
> batchUpdate("updateSingleSomeObject",List<SomeObject>)
>
>
> Think it will make iBatis  more consistent: we have queryForList, i.e.
> getting list of objects in a single line of code, so it's quite natural to
> expect that one should be able to persist the list in a single call.
>
> Wonder what others think about it.
>
> Nicholoz Koka Kiknadze
>