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 fa3506456..00747709a 100644 --- a/src/main/java/org/springframework/samples/petclinic/owner/OwnerController.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/OwnerController.java @@ -98,7 +98,7 @@ class OwnerController { } // find owners by last name - Page ownersResults = findPaginatedForOwnersLastName(page, owner.getLastName()); + Page ownersResults = findPaginatedForOwnersName(page, owner.getLastName()); if (ownersResults.isEmpty()) { // no owners found result.rejectValue("lastName", "notFound", "not found"); @@ -124,10 +124,15 @@ class OwnerController { return "owners/ownersList"; } - private Page findPaginatedForOwnersLastName(int page, String lastname) { + // private Page findPaginatedForOwnersLastName(int page, String lastname) { + // int pageSize = 5; + // Pageable pageable = PageRequest.of(page - 1, pageSize); + // return owners.findByLastNameStartingWith(lastname, pageable); + // } + private Page findPaginatedForOwnersName(int page, String lastname) { int pageSize = 5; Pageable pageable = PageRequest.of(page - 1, pageSize); - return owners.findByLastNameStartingWith(lastname, pageable); + return owners.findByNameContaining(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 5d7a40fbb..ee6ba4222 100644 --- a/src/main/java/org/springframework/samples/petclinic/owner/OwnerRepository.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/OwnerRepository.java @@ -23,6 +23,7 @@ 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.transaction.annotation.Transactional; /** @@ -53,7 +54,10 @@ public interface OwnerRepository extends JpaRepository { * @return a Collection of matching {@link Owner}s (or an empty Collection if none * found) */ - Page findByLastNameStartingWith(String lastName, Pageable pageable); + // Page findByLastNameStartingWith(String lastName, Pageable pageable); + @Query("SELECT o FROM Owner o " + "WHERE LOWER(o.firstName) LIKE LOWER(CONCAT('%', :namePart, '%')) " + + "OR LOWER(o.lastName) LIKE LOWER(CONCAT('%', :namePart, '%'))") + Page findByNameContaining(@Param("namePart") String namePart, Pageable pageable); /** * Retrieve an {@link Owner} from the data store by id. diff --git a/src/main/resources/templates/owners/findOwners.html b/src/main/resources/templates/owners/findOwners.html index 0a818fc79..bb77523eb 100644 --- a/src/main/resources/templates/owners/findOwners.html +++ b/src/main/resources/templates/owners/findOwners.html @@ -9,7 +9,7 @@ class="form-horizontal" id="search-owner-form">
- +
- +