@Test public void findSingleOwner() { Owner owner1 = this.clinicService.findOwnerById(1); assertTrue(owner1.getLastName().startsWith("Franklin")); Owner owner10 = this.clinicService.findOwnerById(10); assertEquals("Carlos", owner10.getFirstName()); assertEquals(owner1.getPets().size(), 1); }
@Test @Transactional public void updateOwner() throws Exception { Owner o1 = this.clinicService.findOwnerById(1); String old = o1.getLastName(); o1.setLastName(old + "X"); this.clinicService.saveOwner(o1); o1 = this.clinicService.findOwnerById(1); assertEquals(old + "X", o1.getLastName()); }
@Override public void save(Owner owner) throws DataAccessException { BeanPropertySqlParameterSource parameterSource = new BeanPropertySqlParameterSource(owner); if (owner.isNew()) { Number newKey = this.insertOwner.executeAndReturnKey(parameterSource); owner.setId(newKey.intValue()); } else { this.namedParameterJdbcTemplate.update( "UPDATE owners SET first_name=:firstName, last_name=:lastName, address=:address, " + "city=:city, telephone=:telephone WHERE id=:id", parameterSource); } }
public void loadPetsAndVisits(final Owner owner) { Map<String, Object> params = new HashMap<String, Object>(); params.put("id", owner.getId().intValue()); final List<JdbcPet> pets = this.namedParameterJdbcTemplate.query( "SELECT id, name, birth_date, type_id, owner_id FROM pets WHERE owner_id=:id", params, new JdbcPetRowMapper()); for (JdbcPet pet : pets) { owner.addPet(pet); pet.setType(EntityUtils.getById(getPetTypes(), PetType.class, pet.getTypeId())); // List<Visit> visits = this.visitRepository.findByPetId(pet.getId()); // for (Visit visit : visits) { // pet.addVisit(visit); // } } }
@Test @Transactional public void insertPet() { Owner owner6 = this.clinicService.findOwnerById(6); int found = owner6.getPets().size(); Pet pet = new Pet(); pet.setName("bowser"); Collection<PetType> types = this.clinicService.findPetTypes(); pet.setType(EntityUtils.getById(types, PetType.class, 2)); pet.setBirthDate(new DateTime()); owner6.addPet(pet); assertEquals(found + 1, owner6.getPets().size()); // both storePet and storeOwner are necessary to cover all ORM tools this.clinicService.savePet(pet); this.clinicService.saveOwner(owner6); owner6 = this.clinicService.findOwnerById(6); assertEquals(found + 1, owner6.getPets().size()); assertNotNull("Pet Id should have been generated", pet.getId()); }
@Override 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; }
@Test @Transactional public void insertOwner() { Collection<Owner> owners = this.clinicService.findOwnerByLastName("Schultz"); int found = owners.size(); Owner owner = new Owner(); owner.setFirstName("Sam"); owner.setLastName("Schultz"); owner.setAddress("4, Evans Street"); owner.setCity("Wollongong"); owner.setTelephone("4444444444"); this.clinicService.saveOwner(owner); Assert.assertNotEquals("Owner Id should have been generated", owner.getId().longValue(), 0); owners = this.clinicService.findOwnerByLastName("Schultz"); assertEquals("Verifying number of owners after inserting a new one.", found + 1, owners.size()); }