From 2ab1cef91f029ba012887d576879fb0a80efda32 Mon Sep 17 00:00:00 2001 From: Perumal Raj A Date: Sat, 22 Feb 2025 05:32:51 +0000 Subject: [PATCH] Query Tunned for the Search Functionality Signed-off-by: Perumal Raj A --- .../petclinic/owner/OwnerController.java | 48 +++++++++++-------- .../petclinic/owner/OwnerRepository.java | 7 +-- src/main/resources/application.properties | 6 ++- 3 files changed, 33 insertions(+), 28 deletions(-) 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 333f717b1..596058d96 100644 --- a/src/main/java/org/springframework/samples/petclinic/owner/OwnerController.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/OwnerController.java @@ -92,27 +92,33 @@ class OwnerController { @GetMapping("/owners") public String processFindForm(@RequestParam(defaultValue = "1") int page, Owner owner, BindingResult result, Model model) { - // allow parameterless GET request for /owners to return all records - if (owner.getLastName() == null) { - owner.setLastName(""); // empty string signifies broadest possible search + try { + // allow parameterless GET request for /owners to return all records + if (owner.getLastName() == null) { + owner.setLastName(""); // empty string signifies broadest possible search + } + + // find owners by last name + Page ownersResults = findPaginatedForOwnersLastName(page, owner.getLastName()); + if (ownersResults.isEmpty()) { + // no owners found + result.rejectValue("lastName", "notFound", "not found"); + return "owners/findOwners"; + } + + if (ownersResults.getTotalElements() == 1) { + // 1 owner found + owner = ownersResults.iterator().next(); + return "redirect:/owners/" + owner.getId(); + } + // multiple owners found + return addPaginationModel(page, model, ownersResults); + } + catch (Exception e) { + e.printStackTrace();// print exception to console or log in file + return "error"; // redirect to generic error page } - // find owners by last name - Page ownersResults = findPaginatedForOwnersName(page, owner.getLastName()); - if (ownersResults.isEmpty()) { - // no owners found - result.rejectValue("lastName", "notFound", "not found"); - return "owners/findOwners"; - } - - if (ownersResults.getTotalElements() == 1) { - // 1 owner found - owner = ownersResults.iterator().next(); - return "redirect:/owners/" + owner.getId(); - } - - // multiple owners found - return addPaginationModel(page, model, ownersResults); } private String addPaginationModel(int page, Model model, Page paginated) { @@ -124,10 +130,10 @@ class OwnerController { return "owners/ownersList"; } - private Page findPaginatedForOwnersName(int page, String lastname) { + private Page findPaginatedForOwnersLastName(int page, String lastname) { int pageSize = 5; Pageable pageable = PageRequest.of(page - 1, pageSize); - return owners.findByNameContaining(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 d8c389da1..3326e64dd 100644 --- a/src/main/java/org/springframework/samples/petclinic/owner/OwnerRepository.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/OwnerRepository.java @@ -27,8 +27,6 @@ import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; import org.springframework.transaction.annotation.Transactional; - - /** * Repository class for Owner domain objects All method names are compliant * with Spring Data naming conventions so this interface can easily be extended for Spring @@ -58,9 +56,8 @@ public interface OwnerRepository extends JpaRepository { * found) */ - @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); + @Query("SELECT o FROM Owner o WHERE LOWER(CONCAT(o.firstName, ' ', o.lastName)) LIKE LOWER(CONCAT('%', :namePart, '%'))") + Page findByLastNameStartingWith(@Param("namePart") String namePart, Pageable pageable); /** * Retrieve an {@link Owner} from the data store by id. diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 6ed985654..4a45d2046 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -18,8 +18,10 @@ management.endpoints.web.exposure.include=* # Logging logging.level.org.springframework=INFO -# logging.level.org.springframework.web=DEBUG -# logging.level.org.springframework.context.annotation=TRACE +logging.level.org.springframework.web=DEBUG +logging.level.org.springframework.context.annotation=TRACE +logging.level.org.springframework=DEBUG +logging.level.org.hibernate.SQL=DEBUG # Maximum time static resources should be cached spring.web.resources.cache.cachecontrol.max-age=12h