Java Reference
In-Depth Information
} else {
sqlMapClient.update("Order.update", order);
sqlMapClient.delete("Order.deleteDetails", order);
for (int i=0;i<order.getOrderItems().size();i++) {
OrderItem oi = (OrderItem) order.getOrderItems().get(i);
sqlMapClient.insert("OrderItem.insert", oi);
} finally {
You may have noticed that we did not start the batch until after the parent record
was already updated (or potentially inserted). The reason is that when you are
using a batched set of statements, database-generated keys will not be generated
until you execute the batch by calling the executeBatch() method. In simpler
terms, this means that if you are using selectKey statements to update your
inserted objects with system-generated keys, they will return null for the gener-
ated keys, and nothing will work as expected. Here is an example:
sqlMapClient.insert("Account.insert", account);
order.setAccountId(account.getAccountId()); // error!
sqlMapClient.insert("Order.insert", order);
In that example, everything looks right: we insert the account, set the generated
key onto the order, and then we insert the order. However, none of the SQL state-
ments execute until you call the executeBatch() method. So, when the third line
executes, the account still has a null value for the accountId property. When the
fourth line executes, the object passed to the insert method has a null accountId ,
too, creating an orphaned record in the order table.
Keep in mind also that batched statements only reuse the PreparedStatement
objects if they are exactly the same as the previous mapped statements. This can
be problematic if you have many mapped statements that you are executing, and
they occur in an order that causes them not to be reused. If you can, execute state-
ments that are the same all together.
Search WWH ::

Custom Search