// Retrieve existing telephones for contact
List<ContactTelDetail> oldContactTelDetails =
contactTelDetailMapper.selectTelDetailForContact(contactId);
for (ContactTelDetail contactTelDetail: oldContactTelDetails) {
ids.add(contactTelDetail.getId());
}
// Update telephone details
if (contact.getContactTelDetails() != null) {
for (ContactTelDetail contactTelDetail:
contact.getContactTelDetails()) {
if (contactTelDetail.getId() == null) {
contactTelDetailMapper.insertContactTelDetail(contactTelDetail);
} else {
contactTelDetailMapper.updateContactTelDetail(contactTelDetail);
ids.remove(contactTelDetail.getId());
}
}
if (ids.size() > 0) {
contactTelDetailMapper.deleteOrphanContactTelDetail(ids);
}
}
// Update hobby details
contactHobbyDetailMapper.deleteHobbyDetailForContact(contact.getId());
if (contact.getHobbies() != null) {
for (Hobby hobby: contact.getHobbies()) {
contactHobbyDetail = new ContactHobbyDetail();
contactHobbyDetail.setContactId(contactId);
contactHobbyDetail.setHobbyId(hobby.getHobbyId());
contactHobbyDetailMapper.insertContactHobbyDetail(contactHobbyDetail);
}
}
return contact;
}
In Listing 11-47, the update logic was implemented in the sequence as described in the beginning of
this section. To test the update operation, add the code snippet in Listing 11-48 to the main() method of
the MyBatisSample class.
Listing 11-48. Testing the save() Method Implementation for the Update Operation
// Update contact
System.out.println("Update contact with id 1");
contact = contactService.findById(1l);
contact.setFirstName("Kim Fung");
Set<ContactTelDetail> contactTels = contact.getContactTelDetails();
ContactTelDetail toDeleteContactTel = null;
for (ContactTelDetail contactTel: contactTels) {
if (contactTel.getTelType().equals("Home")) {
toDeleteContactTel = contactTel;
}
}
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home