Skip to content

Commit

Permalink
wakaleo#149 JdbcPetRepositoryImpl:: findById() simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
arey committed Jun 28, 2016
1 parent 4c72246 commit 078bdc6
Showing 1 changed file with 4 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,26 +82,16 @@ public List<PetType> findPetTypes() throws DataAccessException {

@Override
public Pet findById(int id) throws DataAccessException {
JdbcPet pet;
Integer ownerId;
try {
Map<String, Object> params = new HashMap<>();
params.put("id", id);
pet = this.namedParameterJdbcTemplate.queryForObject(
"SELECT id, name, birth_date, type_id, owner_id FROM pets WHERE id=:id",
params,
new JdbcPetRowMapper());
ownerId = this.namedParameterJdbcTemplate.queryForObject("SELECT owner_id FROM pets WHERE id=:id", params, Integer.class);
} catch (EmptyResultDataAccessException ex) {
throw new ObjectRetrievalFailureException(Pet.class, id);
}
Owner owner = this.ownerRepository.findById(pet.getOwnerId());
owner.addPet(pet);
pet.setType(EntityUtils.getById(findPetTypes(), PetType.class, pet.getTypeId()));

List<Visit> visits = this.visitRepository.findByPetId(pet.getId());
for (Visit visit : visits) {
pet.addVisit(visit);
}
return pet;
Owner owner = this.ownerRepository.findById(ownerId);
return EntityUtils.getById(owner.getPets(), Pet.class, id);
}

@Override
Expand Down

0 comments on commit 078bdc6

Please sign in to comment.