Updating Data

Now that you have learned how to select data from the database, you must be keen to learn how to update existing data. The updates are very similar to selects, we use parameter maps and domain objects extensively. Let's start with the simplest example: updating all columns in the Test table. In Listing 10-31, we modify the sqlMap file for the Test domain objects and add an update statement.

Listing 10-31: sqlMap File for the Test Domain Object

image from book
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN" ¿ "http://www.ibatis.com/dtd/sql-map-2.dtd">   <sqlMap>     <!-- as previous -->          <update  parameter>         update Tests set Name=#name#, RunDate=#runDate# where TestId=#testId#     </update>      </sqlMap>
image from book

As expected, the update SQL statement is in the body of an update element in the sqlMap file, and it does exactly what is expected of it—it updates the Name and RunDate columns in a row identified by TestId and passed in through the int parameterClass. The int parameterClass is an alias for java.lang.Integer, and we can access its int property in #value#.

The implementation in the Java code is slightly more complex; because our TestDao inter- face contains only one method to update or insert the domain object, we need to decide whether the object is to be updated or inserted (see Listing 10-32).

Listing 10-32: TestDao save() iBATIS Implementation

image from book
package com.apress.prospring.ch12.data;      import java.util.List;      import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;      import com.apress.prospring.ch12.domain.Test;      public class SqlMapClientTestDao      extends SqlMapClientDaoSupport      implements TestDao {     private void insert(Test test) {              }          private void update(Test test) {         getSqlMapClientTemplate().update("updateTest", test);     }          public void save(Test test) {         if (test.getTestId() == 0) {             insert(test);         } else {             update(test);         }     }          // other TestDao methods are implemented as stubs }
image from book

Having only a single method for insert and update saved coding, and because our DAO implementation knows how the domain object is stored, it can decide whether to perform an insert or update. Now we are going to ignore the insert(Test) method for the moment and focus on the update(Test) method.

Similar rules to type usage apply to the update statements: you are free to use any Java type or iBATIS alias. To demonstrate this, we create a method that updates the name of a Test record identified by its primary key. Again, we have two options: we can either use a Map instance or the domain object. Tests show that using a Map is slightly slower.[2] When using a Map, we are creating another instance of java.lang.Integer, whereas when we are using the Test domain object, we can use the setTestId(int) method and pass the primitive rather than instance of java.lang.Integer. Listing 10-33 shows two update elements: the first one takes the Test domain object and the second one a Map.

Listing 10-33: sqlMap File for the Test Domain Object

image from book
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN" ¿ "http://www.ibatis.com/dtd/sql-map-2.dtd">   <sqlMap>     <!-- as previous -->          <update bold">updateTestNameDO" parameterbold">test">         update Test set Name=#name# where TestId=#testId#     </update>          <update bold">updateTestNameMap" parameterbold">map">         update Test set Name=#name# where TestId=#testId#     </update> </sqlMap>
image from book

The implementation of the DAO interface is going to be very straightforward (see Listing 10-34). We are not going to discuss the Map implementation; we leave this to you in case you wish to do your own performance testing.

Listing 10-34: TestDao updateName() iBATIS Implementation

image from book
package com.apress.prospring.ch12.data;      import java.util.List;      import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;      import com.apress.prospring.ch12.domain.Test;      public class SqlMapClientTestDao      extends SqlMapClientDaoSupport      implements TestDao {          public void updateName(int testId, String name) {         Test test = new Test();         test.setTestId(testId);         test.setName(name);         getSqlMapClientTemplate().update("updateTestNameDO", test);     }          // other TestDao methods are implemented as stubs } 
image from book

The performance tests on my workstation reveal that the Map implementation took 19,063 ms to perform 5,000 updates, whereas the domain object implementation took 15,937 ms in a table of less than 1,000 rows. Do not be too concerned about using Maps; if your application is performing more complicated updates, the overhead of object creation and lookup is absolutely minimal compared to the actual database work.

There is one final point to make about data updates. Each DAO interface-implementation pair should be responsible for updating its own domain objects. Consider this situation: you have two domain objects: Order and OrderLine. Order has a List that contains instances of OrderLine objects. You may be tempted to code the SQL statement for the save() operation in the OrderDao implementation so that it saves all its OrderLines, but this is not a very good idea. If you do this, the OrderDao and OrderLineDao no longer have clear responsibilities.

You have to document that once OrderDao.save(Order) is called, you do not need to call OrderLineDao.save(OrderLine) for each OrderLine in Order. Also, you probably included the entire operation in a transaction, which is not the best idea because you should try not to control the transactions manually in the code. Delegate transaction management to Spring; if you can't wait to read about transaction management, go to Chapter 12.

[2] It is important to keep in mind that Map is slower in this case. If you find yourself updating only 2 fields out of 20, it is definitely more efficient to use a Map.



Pro Spring
Pro Spring
ISBN: 1590594614
EAN: 2147483647
Year: 2006
Pages: 189

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net