Created
October 12, 2014 21:18
-
-
Save gabanox/0c815eea5d6dab815a94 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static final class ContactWithDetailExtractor implements ResultSetExtractor<List<Contact>> { | |
public List<Contact> extractData(ResultSet rs) throws SQLException, | |
DataAccessException { | |
Map<Long, Contact> map = new HashMap<Long, Contact>(); | |
Contact contact = null; | |
while (rs.next()) { | |
Long id = rs.getLong("id"); | |
contact = map.get(id); | |
if (contact == null) { // new contact record | |
contact = new Contact(); | |
contact.setId(id); | |
contact.setFirstName(rs.getString("first_name")); | |
contact.setLastName(rs.getString("last_name")); | |
contact.setBirthDate(rs.getDate("birth_date")); | |
contact.setContactTelDetails(new ArrayList<ContactTelDetail>()); | |
map.put(id, contact); | |
} | |
// Process contact tel. detail (if exists) | |
Long contactTelDetailId = rs.getLong("contact_tel_id"); | |
if (contactTelDetailId > 0) { | |
ContactTelDetail contactTelDetail = new ContactTelDetail(); | |
contactTelDetail.setId(contactTelDetailId); | |
contactTelDetail.setContactId(id); | |
contactTelDetail.setTelType(rs.getString("tel_type")); | |
contactTelDetail.setTelNumber(rs.getString("tel_number")); | |
contact.getContactTelDetails().add(contactTelDetail); | |
} | |
} | |
return new ArrayList<Contact> (map.values()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment