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.