Batch Insert

13 views
Skip to first unread message

Cristian Vrabie

unread,
Aug 2, 2012, 2:08:02 PM8/2/12
to orbr...@googlegroups.com
I'm curious if Transactional.executeBatch can be used to perform a batch insert like this: 

INSERT
INTO    mytable (col1, col2, col3)
VALUES (col1_value1,
col2_value1, col3_value1), (col1_value2, col2_value2, col3_value2)

Correct me if I'm just being foolish, but this would require us to pass to it an Iterable[Map[String,Any]], wouldn't in? Basically for each new row a map of columnName->columnValue . 
However, executeBatch takes a parameter batchValues:(String,Traversable[_]) and a parameter parms:Map[String,Any]. Looking at the code it seems that it uses params for all rows to which it glues one extra, unique, column from batchValues. This would be for rows that have all but one column identical. So:

INSERT
INTO    mytable (col1, col2, col3)
VALUES (col1_value,
col2_value, col3_value1), (col1_value, col2_value, col3_value2)

Is my understanding of how this works correct? And if yes, is it possible to actually specify different values for all columns?

Thanks!
Cristian

Nils Kilden-Pedersen

unread,
Aug 2, 2012, 2:19:35 PM8/2/12
to orbr...@googlegroups.com
Yes, like this:

INSERT
INTO    mytable (col1, col2, col3)
VALUES  (:batchObj.col1_value, :batchObj.col2_value, :batchObj.col3_value)
and then e.g.

val batchValues: Seq[Whatever] = ... // "Whatever" has vals/defs col1_value, col2_value, etc.
session.executeBatch(token, "batchObj"->batchValues)

HTH,

Nils

Cristian Vrabie

unread,
Aug 2, 2012, 4:15:04 PM8/2/12
to orbr...@googlegroups.com
Whoa! I didn't knew it explores the objects passed in the batch parameter.
This is really nice! What does it use? Reflection?

Thanks!
Cristian


On Thursday, 2 August 2012 19:19:35 UTC+1, Nils Kilden-Pedersen wrote:
Yes, like this:
INSERT
INTO    mytable (col1, col2, col3)
VALUES (:batchObj.col1_value, 
:batchObj.col2_value, :batchObj.col3_value)
and then e.g.
val batchValues: Seq[Whatever] = ... // "Whatever" has vals/defs col1_value, col2_value, etc.
session.executeBatch(token, "batchObj"->batchValues)

HTH,

Nils


On Thu, Aug 2, 2012 at 1:08 PM:

Nils Kilden-Pedersen

unread,
Aug 2, 2012, 5:55:46 PM8/2/12
to orbr...@googlegroups.com
On Thu, Aug 2, 2012 at 3:15 PM, Cristian Vrabie <cristia...@gmail.com> wrote:
Whoa! I didn't knew it explores the objects passed in the batch parameter.
This is really nice! What does it use? Reflection?

Yes.

Reply all
Reply to author
Forward
0 new messages