mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-15 12:25:50 +00:00
#149 JdbcPetRepositoryImpl:: findById() simplification
This commit is contained in:
parent
4c722465d8
commit
078bdc6cfb
1 changed files with 4 additions and 14 deletions
|
@ -82,26 +82,16 @@ public class JdbcPetRepositoryImpl implements PetRepository {
|
|||
|
||||
@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
|
||||
|
|
Loading…
Reference in a new issue