Ejemplo n.º 1
0
 @Transactional(readOnly = true)
 public Pet loadPet(int id) throws DataAccessException {
   JdbcPet pet;
   try {
     pet =
         this.simpleJdbcTemplate.queryForObject(
             "SELECT id, name, birth_date, type_id, owner_id FROM pets WHERE id=?",
             new JdbcPetRowMapper(),
             id);
   } catch (EmptyResultDataAccessException ex) {
     throw new ObjectRetrievalFailureException(Pet.class, new Integer(id));
   }
   Owner owner = loadOwner(pet.getOwnerId());
   owner.addPet(pet);
   pet.setType(EntityUtils.getById(getPetTypes(), PetType.class, pet.getTypeId()));
   loadVisits(pet);
   return pet;
 }
  public Pet findById(int id) throws DataAccessException {
    JdbcPet pet;
    try {
      Map<String, Object> params = new HashMap<String, Object>();
      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());
    } catch (EmptyResultDataAccessException ex) {
      throw new ObjectRetrievalFailureException(Pet.class, new Integer(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;
  }