/** * Creates a {@link MapSqlParameterSource} based on data values from the supplied {@link Pet} * instance. */ private MapSqlParameterSource createPetParameterSource(Pet pet) { return new MapSqlParameterSource() .addValue("id", pet.getId()) .addValue("name", pet.getName()) .addValue("birth_date", pet.getBirthDate()) .addValue("type_id", pet.getType().getId()) .addValue("owner_id", pet.getOwner().getId()); }
@RequestMapping(method = RequestMethod.GET) public String setupForm(@RequestParam("petId") int petId, Model model) { Pet pet = this.clinic.loadPet(petId); Visit visit = new Visit(); pet.addVisit(visit); model.addAttribute("visit", visit); return "visitForm"; }
@Transactional public void storePet(Pet pet) throws DataAccessException { if (pet.isNew()) { Number newKey = this.insertPet.executeAndReturnKey(createPetParameterSource(pet)); pet.setId(newKey.intValue()); } else { this.simpleJdbcTemplate.update( "UPDATE pets SET name=:name, birth_date=:birth_date, type_id=:type_id, " + "owner_id=:owner_id WHERE id=:id", createPetParameterSource(pet)); } }
/** Method creates a new <code>Visit</code> with the correct <code>Pet</code> info */ protected Object formBackingObject(HttpServletRequest request) throws ServletException { Pet pet = getClinic().loadPet(ServletRequestUtils.getRequiredIntParameter(request, "petId")); Visit visit = new Visit(); pet.addVisit(visit); return visit; }