Hello Ingmar,

I have gone through the link you gave me here is what is been given there

<insert id="InsertOrganization" parameterClass="Organization" resultClass="int">
        <selectKey property="Id" type="post" resultClass="int">
                SELECT cast(last_value as int) AS value
                FROM organizations_org_id_seq
        </selectKey>
        INSERT INTO Organizations
                (Org_Code, Org_Name)
        VALUES
                (#Code#, #Name#)
</insert>


What I can understand from above lines that I can select the last generated key 
my question is how I can use this last generated key as a foreign key to 
another table (that table is referring to nothing but child object of 
Organization class).

So how will this work that while inserting my
For the parent object say data I will call MSCPerfCntrMscMMSTblImpl and insert 
into MSCPerfCntrMscMMSTbl but my MSCPerfCntrMscMMSTblImpl object contains a 
child object which contains data for another table so at my application level 
do I have to code in following manner using Batch process?


public void saveOrder(SqlMapClient sqlMapClient, Order order)
throws SQLException {
sqlMapClient.startTransaction();
try {
if (null == order.getOrderId()) {
sqlMapClient.insert("Order.insert", order);
} else {
sqlMapClient.update("Order.update", order);
}
sqlMapClient.startBatch();
sqlMapClient.delete("Order.deleteDetails", order);
for (int i=0;i<order.getOrderItems().size();i++) {
OrderItem oi = (OrderItem) order.getOrderItems().get(i);
oi.setOrderId(order.getOrderId());
sqlMapClient.insert("OrderItem.insert", oi);
}
sqlMapClient.executeBatch();
sqlMapClient.commitTransaction();
} finally {
sqlMapClient.endTransaction();
}
}


Hope my question are clear, if not please let me know I will try to do so in 
better manner.


Looking forward to your response

Regards
Rahul Saluja

-----Original Message-----
From: Ingmar Lötzsch [mailto:[email protected]]
Sent: Monday, February 09, 2009 2:15 PM
To: [email protected]
Subject: Re: Need help on Inserting child Object .

Hello Rahul,

> I am Using postgresSql As my DB and I am using auto-generated  keys as my way 
> to create new ID which is as follows:
>
> <insert id="MSCPerfCntrMscMMSTblImpl"
> parameterClass="com.hns.hss.nmf.server.log.manager.gensrc.MSCPerformance.impl.MSCPerfCntrMscMMSTblImpl">
>         insert into MSCPerfCntrMscMMSTbl(
>         seq_no,
>         neinfo_id,
>         rectimeStamp,
>         index,
>         numClsMrkUpdates,
>         ciphrModeCntrlAttempt,
>         succCiphrModeCntrl
>         )
> values
>         (
>         nextval ('MSCPerfCntrMscMMS_seq'),
>         #neInfoId_0#,
>         #timeStamp_0#,
>         #index_1.value#,
>         #numClsMrkUpdates_2.value#,
>         #ciphrModeCntrlAttempt_3.value#,
>         #succCiphrModeCntrl_4.value#)
> </insert>

You can use the <selectKey> element as described in

http://opensource.atlassian.com/confluence/oss/pages/viewpage.action?pageId=407

I'am sure, there is a chapter in "iBATIS in Action" which describes this
feature too.

If your class MSCPerfCntrMscMMSTblImpl contains an instance variable of
type int or Integer and with identifier "id" and corresponding
getters/setters, this looks like

<insert id="MSCPerfCntrMscMMSTblImpl"
parameterClass="com.hns.hss.nmf.server.log.manager.gensrc.MSCPerformance.impl.MSCPerfCntrMscMMSTblImpl">
        <selectKey keyProperty="id" resultClass="int" type="pre">
                nextval('MSCPerfCntrMscMMS_seq')
        </selectKey>
        insert into MSCPerfCntrMscMMSTbl(
        seq_no,
        neinfo_id,
        rectimeStamp,
        index,
        numClsMrkUpdates,
        ciphrModeCntrlAttempt,
        succCiphrModeCntrl
        )
values
        (
        #id#,
        #neInfoId_0#,
        #timeStamp_0#,
        #index_1.value#,
        #numClsMrkUpdates_2.value#,
        #ciphrModeCntrlAttempt_3.value#,
        #succCiphrModeCntrl_4.value#)
</insert>

Ingmar
The information contained in this e-mail is private & confidential and may also 
be legally privileged. If you are not the intended recipient, please notify us, 
preferably by e-mail, and do not read, copy or disclose the contents of this 
message to anyone.

Reply via email to