/** * 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().toDate()) .addValue("type_id", pet.getType().getId()) .addValue("owner_id", pet.getOwner().getId()); }
@Test @Transactional public void updatePet() throws Exception { Pet pet7 = this.clinicService.findPetById(7); String old = pet7.getName(); pet7.setName(old + "X"); this.clinicService.savePet(pet7); pet7 = this.clinicService.findPetById(7); assertEquals(old + "X", pet7.getName()); }
@Override public void save(Pet pet) throws DataAccessException { if (pet.isNew()) { Number newKey = this.insertPet.executeAndReturnKey(createPetParameterSource(pet)); pet.setId(newKey.intValue()); } else { this.namedParameterJdbcTemplate.update( "UPDATE pets SET name=:name, birth_date=:birth_date, type_id=:type_id, owner_id=:owner_id WHERE id=:id", createPetParameterSource(pet)); } }
/** * Return the Pet with the given name, or null if none found for this Owner. * * @param name to test * @return true if pet name is already in use */ @Nullable public Pet getPet(String name, boolean ignoreNew) { name = name.toLowerCase(); for (Pet pet : getPetsInternal()) { if (!ignoreNew || !pet.isNew()) { String compName = pet.getName(); if (compName != null && compName.toLowerCase().equals(name)) { return pet; } } } return null; }
@Test @Transactional public void insertVisit() { Pet pet7 = this.clinicService.findPetById(7); int found = pet7.getVisits().size(); Visit visit = new Visit(); pet7.addVisit(visit); visit.setDescription("test"); // both storeVisit and storePet are necessary to cover all ORM tools this.clinicService.saveVisit(visit); this.clinicService.savePet(pet7); pet7 = this.clinicService.findPetById(7); assertEquals(found + 1, pet7.getVisits().size()); assertNotNull("Visit Id should have been generated", visit.getId()); }
@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()); }
@Test public void findPet() { Collection<PetType> types = this.clinicService.findPetTypes(); Pet pet7 = this.clinicService.findPetById(7); assertTrue(pet7.getName().startsWith("Samantha")); assertEquals(EntityUtils.getById(types, PetType.class, 1).getId(), pet7.getType().getId()); assertEquals("Jean", pet7.getOwner().getFirstName()); Pet pet6 = this.clinicService.findPetById(6); assertEquals("George", pet6.getName()); assertEquals(EntityUtils.getById(types, PetType.class, 4).getId(), pet6.getType().getId()); assertEquals("Peter", pet6.getOwner().getFirstName()); }
public void addPet(Pet pet) { getPetsInternal().add(pet); pet.setOwner(this); }