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 Rahul Saluja <ra...@vnl.in> on 2009/02/08 12:05:48 UTC

Need help on Inserting child Object .

Hello Everybody,

 I need help to understand how can I insert an object into my Db using which is having an child object.

Say I have a class named Order which have an Child Object (an array of type OrderItem ) so how I would write my code in java to associate these two tables
Namely Order
Class Order{

public void setOrderItemArray(OrderItem[] newArray);
public OrderItem[] getOrderItemArray();
public void setOrderItem(int index, OrderItem oi);
public OrderItem getOrderItem(int index);
}

and OrderItem

Class OrderItem{

Public void SetOrderItemPrice( int price);
Public int getOrderItemPrice( String OrderItemName );

Public void setOrderItemNumber(int OrderItemName);
Public int getOrderItemNumber( String ItemName);


Public void setOrderItemName(String Name);
Public String getOrderItemName(String Name);

}

I was Going through book Ibatis in Action and found this example under the topic inserting or updating child object

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();
}
}

As I was going through above code I have few question why are checking it against null for insert , shouldn't that be checking for not null

Though I can understand that It needs to be done at application level only using sql batch update but an explicit example of the same with corresponding entries in sqlmap would help . If somebody can put some light on this would help me in great manner.

Looking forward to your response.

Regards
Rahul Saluja
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.

Re: Need help on Inserting child Object .

Posted by Ingmar Lötzsch <il...@asci-systemhaus.de>.
Hello Rahul,

> Well I could not understand what you actually mean to say by this
> 
> " This works only, if there are no depedencies to the child table and if you don't have to log the changes "

This refers to the use of DELETE during the UPDATE only. If there is a
third table with

FOREIGN KEY (orderitemid)
REFERENCES orderitem(id)
ON DELETE RESTRICT (respectively CASCADE)

the

DELETE FROM orderitem
WHERE orderid = #value#

could cause a FOREIGN KEY violation respectively the deletion of some
rows in the third table. In your case this seems irrelevant.

>> sqlMapClient.delete("Order.deleteDetails", order);
> 
> This works only, if there are no depedencies to the child table and if
> you don't have to log the changes.
> 
>> 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);
>> }

Ingmar

RE: Need help on Inserting child Object .

Posted by Rahul Saluja <ra...@vnl.in>.
Hi Ingmar,

Thanks for replying and being so patient in answering my queries,

Well I could not understand what you actually mean to say by this

" This works only, if there are no depedencies to the child table and if you don't have to log the changes "

Do you mean to say that I should not run update query on my child table, well all I need to do is insert the data of parent object and child object in corresponding tables but no update , as all this data will be used just for reporting.

I could not understand what you were pointing to while stating ---> "if there are no dependencies to the child table"

Looking forward to your response.

Regards
Rahul Saluja

-----Original Message-----
From: Ingmar Lötzsch [mailto:iloetzsch@asci-systemhaus.de]
Sent: Monday, February 09, 2009 4:30 PM
To: user-java@ibatis.apache.org
Subject: Re: Need help on Inserting child Object .

Hello Rahul,

> 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?

If your database is normalized, yes. As far as I know. If you have an
array of an userdefined type MSCPerfCntrMscMMSTbl, you have to act
different.

> 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);

This works only, if there are no depedencies to the child table and if
you don't have to log the changes.

> 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();
> }
> }

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.

Re: Need help on Inserting child Object .

Posted by Ingmar Lötzsch <il...@asci-systemhaus.de>.
Hello Rahul,

> 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?

If your database is normalized, yes. As far as I know. If you have an
array of an userdefined type MSCPerfCntrMscMMSTbl, you have to act
different.

> 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);

This works only, if there are no depedencies to the child table and if
you don't have to log the changes.

> 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();
> }
> }

Ingmar

RE: Need help on Inserting child Object .

Posted by Rahul Saluja <ra...@vnl.in>.
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:iloetzsch@asci-systemhaus.de]
Sent: Monday, February 09, 2009 2:15 PM
To: user-java@ibatis.apache.org
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.

Re: Need help on Inserting child Object .

Posted by Ingmar Lötzsch <il...@asci-systemhaus.de>.
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

RE: Need help on Inserting child Object .

Posted by Rahul Saluja <ra...@vnl.in>.
Hello IngMar,

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>


Looking forward to your response.

Regards
Rahul Saluja

-----Original Message-----
From: Ingmar Lötzsch [mailto:iloetzsch@asci-systemhaus.de]
Sent: Monday, February 09, 2009 4:09 AM
To: user-java@ibatis.apache.org
Subject: Re: Need help on Inserting child Object .

Hello, Rahul

> if (null == order.getOrderId()) {
> sqlMapClient.insert("Order.insert", order);
> } else {
> sqlMapClient.update("Order.update", order);
> }

> As I was going through above code I have few question why are checking
> it against null for insert , shouldn't that be checking for not null
>
> Though I can understand that It needs to be done at application level
> only using sql batch update but an explicit example of the same with
> corresponding entries in sqlmap would help . If somebody can put some
> light on this would help me in great manner.

what DBMS you are using? What is your application's/DB's way to create
new IDs?

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.

Re: Need help on Inserting child Object .

Posted by Ingmar Lötzsch <il...@asci-systemhaus.de>.
Hello, Rahul

> if (null == order.getOrderId()) {
> sqlMapClient.insert("Order.insert", order);
> } else {
> sqlMapClient.update("Order.update", order);
> }

> As I was going through above code I have few question why are checking
> it against null for insert , shouldn’t that be checking for not null
> 
> Though I can understand that It needs to be done at application level
> only using sql batch update but an explicit example of the same with
> corresponding entries in sqlmap would help . If somebody can put some
> light on this would help me in great manner.

what DBMS you are using? What is your application's/DB's way to create
new IDs?

Ingmar