The new insertWithDetail() method will insert both the contact and its telephone details into the
database.
To be able to insert the telephone detail record, we need to create the InsertContactTelDetail class,
which was shown in Listing 8-47. :InsertContactTelDetail class
Listing 8-47. The InsertContactTelDetail Class
package com.apress.prospring3.ch8.dao.jdbc.annotation;
import java.sql.Types;
import javax.sql.DataSource;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.object.BatchSqlUpdate;
public class InsertContactTelDetail extends BatchSqlUpdate {
private static final String SQL_INSERT_CONTACT_TEL =
"insert into contact_tel_detail (contact_id, tel_type, tel_number) values
(:contact_id, :tel_type, :tel_number)";
private static final int BATCH_SIZE = 10;
public InsertContactTelDetail(DataSource dataSource) {
super(dataSource, SQL_INSERT_CONTACT_TEL);
declareParameter(new SqlParameter("contact_id", Types.INTEGER));
declareParameter(new SqlParameter("tel_type", Types.VARCHAR));
declareParameter(new SqlParameter("tel_number", Types.VARCHAR));
setBatchSize(BATCH_SIZE);
}
}
Note that in the constructor, we called the BatchSqlUpdate.setBatchSize() method to set the batch
size for the JDBC insert operation.
Listing 8-48 shows the implementation of the insertWithDetail() method in the JdbcContactDao
class.
Listing 8-48. Batch SQL Update Operation
package com.apress.prospring3.ch8.dao.jdbc.annotation;
// Import statements omitted
@Repository("contactDao")
public class JdbcContactDao implements ContactDao {
private InsertContactTelDetail insertContactTelDetail;
public void insertWithDetail(Contact contact) {
insertContactTelDetail = new InsertContactTelDetail(dataSource);
// Insert contact
Map<String, Object> paramMap = new HashMap<String, Object>();
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home