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 Deepinder <dm...@zymesolutions.com> on 2007/01/05 15:11:24 UTC

Data insertion into Parent and Child table -- How to do this while batch insert in IBatis

 

Hi,

 

Is it possible to insert data into a parent table and then use the generated
primary key to insert into a child table.

I need to do this during a batch insert as it is necessary(performance).

 

Thanking you for any lead that may be provided in solving this problem.(I
spent a day , on this problem :-))

 

The sample code below has a comment specifying where the problem is

 

public void batchInsertTable(

                                    final List beanList) {

 

                        getSqlMapClientTemplate().execute(new
SqlMapClientCallback() {

 

                                    public Object
doInSqlMapClient(SqlMapExecutor executor)

                                                            throws
SQLException {

 

                                                executor.startBatch();

 

                                                for (int i = 0; i <
beanList.size(); i++) {

                                                            Integer
primaryKey = (Integer)executor.insert("populateTable",

 
beanList.get(i));

                                                            //use primaryKey
for storing info in child table

//Problem is that in batch insert the primarykey value returned is random
number (not the //one in database)

                                                }

 

                                                executor.executeBatch();

                                                return null;

                                    }

 

                        });

 

            }


Re: Data insertion into Parent and Child table -- How to do this while batch insert in IBatis

Posted by Larry Meadors <lm...@apache.org>.
IIRC, your batch will be lots faster in a transaction, and if you do this:

start transaction
  insert parent
  start batch
    insert children
  end batch
end transaction

I think you'll be OK.

Larry


On 1/5/07, Deepinder <dm...@zymesolutions.com> wrote:
>
>
>
>
>
>
> Hi,
>
>
>
> Is it possible to insert data into a parent table and then use the generated
> primary key to insert into a child table.
>
> I need to do this during a batch insert as it is necessary(performance).
>
>
>
> Thanking you for any lead that may be provided in solving this problem.(I
> spent a day , on this problem J)
>
>
>
> The sample code below has a comment specifying where the problem is
>
>
>
> public void batchInsertTable(
>
>                                     final List beanList) {
>
>
>
>
> getSqlMapClientTemplate().execute(new
> SqlMapClientCallback() {
>
>
>
>                                     public Object
> doInSqlMapClient(SqlMapExecutor executor)
>
>                                                             throws
> SQLException {
>
>
>
>                                                 executor.startBatch();
>
>
>
>                                                 for (int i = 0; i <
> beanList.size(); i++) {
>
>                                                             Integer
> primaryKey = (Integer)executor.insert("populateTable",
>
>
>        beanList.get(i));
>
>
> //use primaryKey for storing info in child table
>
> //Problem is that in batch insert the primarykey value returned is random
> number (not the //one in database)
>
>                                                 }
>
>
>
>                                                 executor.executeBatch();
>
>                                                 return null;
>
>                                     }
>
>
>
>                         });
>
>
>
>             }