diff --git a/src/main/java/org/springframework/samples/petclinic/owner/OwnerController.java b/src/main/java/org/springframework/samples/petclinic/owner/OwnerController.java index 463a94c73..44c85a62a 100644 --- a/src/main/java/org/springframework/samples/petclinic/owner/OwnerController.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/OwnerController.java @@ -127,7 +127,7 @@ class OwnerController { private Page findPaginatedForOwnersLastName(int page, String lastname) { int pageSize = 5; Pageable pageable = PageRequest.of(page - 1, pageSize); - return owners.findByLastName(lastname, pageable); + return owners.findByLastNameStartingWith(lastname, pageable); } @GetMapping("/owners/{ownerId}/edit") diff --git a/src/main/java/org/springframework/samples/petclinic/owner/OwnerRepository.java b/src/main/java/org/springframework/samples/petclinic/owner/OwnerRepository.java index 7bbb91e93..63fae196f 100644 --- a/src/main/java/org/springframework/samples/petclinic/owner/OwnerRepository.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/OwnerRepository.java @@ -19,13 +19,11 @@ import java.util.List; import java.util.Optional; import jakarta.annotation.Nonnull; -import jakarta.validation.constraints.NotNull; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; -import org.springframework.lang.NonNullApi; import org.springframework.transaction.annotation.Transactional; /** @@ -57,9 +55,7 @@ public interface OwnerRepository extends JpaRepository { * @return a Collection of matching {@link Owner}s (or an empty Collection if none * found) */ - @Query("SELECT DISTINCT owner FROM Owner owner left join owner.pets WHERE owner.lastName LIKE :lastName% ") - @Transactional(readOnly = true) - Page findByLastName(@Param("lastName") String lastName, Pageable pageable); + Page findByLastNameStartingWith(String lastName, Pageable pageable); /** * Retrieve an {@link Owner} from the data store by id. @@ -79,8 +75,6 @@ public interface OwnerRepository extends JpaRepository { /** * Returns all the owners from data store **/ - @Query("SELECT owner FROM Owner owner") - @Transactional(readOnly = true) Page findAll(Pageable pageable); } diff --git a/src/main/java/org/springframework/samples/petclinic/owner/PetController.java b/src/main/java/org/springframework/samples/petclinic/owner/PetController.java index f5dc7a358..fcf431bff 100644 --- a/src/main/java/org/springframework/samples/petclinic/owner/PetController.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/PetController.java @@ -62,9 +62,6 @@ class PetController { Optional optionalOwner = this.owners.findById(ownerId); Owner owner = optionalOwner.orElseThrow(() -> new IllegalArgumentException( "Owner not found with id: " + ownerId + ". Please ensure the ID is correct ")); - if (owner == null) { - throw new IllegalArgumentException("Owner ID not found: " + ownerId); - } return owner; } @@ -79,9 +76,6 @@ class PetController { Optional optionalOwner = this.owners.findById(ownerId); Owner owner = optionalOwner.orElseThrow(() -> new IllegalArgumentException( "Owner not found with id: " + ownerId + ". Please ensure the ID is correct ")); - if (owner == null) { - throw new IllegalArgumentException("Owner ID not found: " + ownerId); - } return owner.getPet(petId); } @@ -99,7 +93,6 @@ class PetController { public String initCreationForm(Owner owner, ModelMap model) { Pet pet = new Pet(); owner.addPet(pet); - model.put("pet", pet); return VIEWS_PETS_CREATE_OR_UPDATE_FORM; } @@ -126,7 +119,7 @@ class PetController { } @GetMapping("/pets/{petId}/edit") - public String initUpdateForm(Owner owner, @PathVariable("petId") int petId, RedirectAttributes redirectAttributes) { + public String initUpdateForm() { return VIEWS_PETS_CREATE_OR_UPDATE_FORM; } diff --git a/src/test/java/org/springframework/samples/petclinic/owner/OwnerControllerTests.java b/src/test/java/org/springframework/samples/petclinic/owner/OwnerControllerTests.java index 40a40bc54..c285f39f5 100644 --- a/src/test/java/org/springframework/samples/petclinic/owner/OwnerControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/owner/OwnerControllerTests.java @@ -91,7 +91,7 @@ class OwnerControllerTests { void setup() { Owner george = george(); - given(this.owners.findByLastName(eq("Franklin"), any(Pageable.class))) + given(this.owners.findByLastNameStartingWith(eq("Franklin"), any(Pageable.class))) .willReturn(new PageImpl<>(Lists.newArrayList(george))); given(this.owners.findAll(any(Pageable.class))).willReturn(new PageImpl<>(Lists.newArrayList(george))); @@ -144,14 +144,14 @@ class OwnerControllerTests { @Test void testProcessFindFormSuccess() throws Exception { Page tasks = new PageImpl<>(Lists.newArrayList(george(), new Owner())); - when(this.owners.findByLastName(anyString(), any(Pageable.class))).thenReturn(tasks); + when(this.owners.findByLastNameStartingWith(anyString(), any(Pageable.class))).thenReturn(tasks); mockMvc.perform(get("/owners?page=1")).andExpect(status().isOk()).andExpect(view().name("owners/ownersList")); } @Test void testProcessFindFormByLastName() throws Exception { Page tasks = new PageImpl<>(Lists.newArrayList(george())); - when(this.owners.findByLastName(eq("Franklin"), any(Pageable.class))).thenReturn(tasks); + when(this.owners.findByLastNameStartingWith(eq("Franklin"), any(Pageable.class))).thenReturn(tasks); mockMvc.perform(get("/owners?page=1").param("lastName", "Franklin")) .andExpect(status().is3xxRedirection()) .andExpect(view().name("redirect:/owners/" + TEST_OWNER_ID)); @@ -160,7 +160,7 @@ class OwnerControllerTests { @Test void testProcessFindFormNoOwnersFound() throws Exception { Page tasks = new PageImpl<>(Lists.newArrayList()); - when(this.owners.findByLastName(eq("Unknown Surname"), any(Pageable.class))).thenReturn(tasks); + when(this.owners.findByLastNameStartingWith(eq("Unknown Surname"), any(Pageable.class))).thenReturn(tasks); mockMvc.perform(get("/owners?page=1").param("lastName", "Unknown Surname")) .andExpect(status().isOk()) .andExpect(model().attributeHasFieldErrors("owner", "lastName")) diff --git a/src/test/java/org/springframework/samples/petclinic/service/ClinicServiceTests.java b/src/test/java/org/springframework/samples/petclinic/service/ClinicServiceTests.java index 39a444e2d..17360278f 100644 --- a/src/test/java/org/springframework/samples/petclinic/service/ClinicServiceTests.java +++ b/src/test/java/org/springframework/samples/petclinic/service/ClinicServiceTests.java @@ -82,10 +82,10 @@ class ClinicServiceTests { @Test void shouldFindOwnersByLastName() { - Page owners = this.owners.findByLastName("Davis", pageable); + Page owners = this.owners.findByLastNameStartingWith("Davis", pageable); assertThat(owners).hasSize(2); - owners = this.owners.findByLastName("Daviss", pageable); + owners = this.owners.findByLastNameStartingWith("Daviss", pageable); assertThat(owners).isEmpty(); } @@ -103,7 +103,7 @@ class ClinicServiceTests { @Test @Transactional void shouldInsertOwner() { - Page owners = this.owners.findByLastName("Schultz", pageable); + Page owners = this.owners.findByLastNameStartingWith("Schultz", pageable); int found = (int) owners.getTotalElements(); Owner owner = new Owner(); @@ -115,7 +115,7 @@ class ClinicServiceTests { this.owners.save(owner); assertThat(owner.getId()).isNotZero(); - owners = this.owners.findByLastName("Schultz", pageable); + owners = this.owners.findByLastNameStartingWith("Schultz", pageable); assertThat(owners.getTotalElements()).isEqualTo(found + 1); }