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 ab594841c..f9cade39d 100644 --- a/src/main/java/org/springframework/samples/petclinic/owner/OwnerRepository.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/OwnerRepository.java @@ -54,6 +54,30 @@ public interface OwnerRepository extends JpaRepository { */ Page findByLastNameStartingWith(String lastName, Pageable pageable); + /** + * Retrieve {@link Owner}s from the data store whose pets' names contain the given name. + * @param petName Value to search for in pet names (supports fuzzy search) + * @param pageable pagination information + * @return a Collection of matching {@link Owner}s (or an empty Collection if none + * found) + */ + @Query("SELECT DISTINCT owner FROM Owner owner JOIN owner.pets pet WHERE pet.name LIKE %:petName%") + Page findByPetNameContaining(@org.springframework.data.repository.query.Param("petName") String petName, Pageable pageable); + + /** + * Retrieve {@link Owner}s from the data store by combining last name and pet name search. + * @param lastName Value to search for in last names + * @param petName Value to search for in pet names + * @param pageable pagination information + * @return a Collection of matching {@link Owner}s (or an empty Collection if none + * found) + */ + @Query("SELECT DISTINCT owner FROM Owner owner JOIN owner.pets pet WHERE owner.lastName LIKE %:lastName% AND pet.name LIKE %:petName%") + Page findByLastNameAndPetName( + @org.springframework.data.repository.query.Param("lastName") String lastName, + @org.springframework.data.repository.query.Param("petName") String petName, + Pageable pageable); + /** * Retrieve an {@link Owner} from the data store by id. *

@@ -74,4 +98,4 @@ public interface OwnerRepository extends JpaRepository { **/ Page findAll(Pageable pageable); -} +} \ No newline at end of file