mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-15 20:35:49 +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
|
@Override
|
||||||
public Pet findById(int id) throws DataAccessException {
|
public Pet findById(int id) throws DataAccessException {
|
||||||
JdbcPet pet;
|
Integer ownerId;
|
||||||
try {
|
try {
|
||||||
Map<String, Object> params = new HashMap<>();
|
Map<String, Object> params = new HashMap<>();
|
||||||
params.put("id", id);
|
params.put("id", id);
|
||||||
pet = this.namedParameterJdbcTemplate.queryForObject(
|
ownerId = this.namedParameterJdbcTemplate.queryForObject("SELECT owner_id FROM pets WHERE id=:id", params, Integer.class);
|
||||||
"SELECT id, name, birth_date, type_id, owner_id FROM pets WHERE id=:id",
|
|
||||||
params,
|
|
||||||
new JdbcPetRowMapper());
|
|
||||||
} catch (EmptyResultDataAccessException ex) {
|
} catch (EmptyResultDataAccessException ex) {
|
||||||
throw new ObjectRetrievalFailureException(Pet.class, id);
|
throw new ObjectRetrievalFailureException(Pet.class, id);
|
||||||
}
|
}
|
||||||
Owner owner = this.ownerRepository.findById(pet.getOwnerId());
|
Owner owner = this.ownerRepository.findById(ownerId);
|
||||||
owner.addPet(pet);
|
return EntityUtils.getById(owner.getPets(), Pet.class, id);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in a new issue