From e430dba0022eb1ef524de2669eb41bf1bd925252 Mon Sep 17 00:00:00 2001 From: Shay Keren Date: Wed, 30 Jul 2025 11:47:56 +0300 Subject: [PATCH 1/2] chore: change pets fetch type to lazy and add batch size optimization --- .../samples/petclinic/owner/Owner.java | 20 +++++-------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/src/main/java/org/springframework/samples/petclinic/owner/Owner.java b/src/main/java/org/springframework/samples/petclinic/owner/Owner.java index aab7772b67f..037083433af 100644 --- a/src/main/java/org/springframework/samples/petclinic/owner/Owner.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/Owner.java @@ -16,9 +16,7 @@ package org.springframework.samples.petclinic.owner; import java.util.ArrayList; -import java.util.List; - -import org.springframework.core.style.ToStringCreator; +import java.util.List;import org.springframework.core.style.ToStringCreator; import org.springframework.samples.petclinic.model.Person; import org.springframework.util.Assert;import jakarta.persistence.CascadeType; import jakarta.persistence.Column; @@ -49,9 +47,7 @@ @Column(name = "city") @NotBlank - private String city; - - @Column(name = "telephone") + private String city;@Column(name = "telephone") @NotBlank @Digits(fraction = 0, integer = 10) private String telephone; @@ -88,9 +84,7 @@ public void setTelephone(String telephone) { public List getPets() { return this.pets; - } - - public void addPet(Pet pet) { + }public void addPet(Pet pet) { if (pet.isNew()) { getPets().add(pet); } @@ -101,9 +95,7 @@ public void addPet(Pet pet) { */ public Pet getPet(String name) { return getPet(name, false); - } - - /** + }/** * Return the Pet with the given id, or null if none found for this Owner. * @param id to test * @return a pet if pet id is already in use @@ -134,9 +126,7 @@ public Pet getPet(String name, boolean ignoreNew) { } } return null; - } - - @Override + }@Override public String toString() { return new ToStringCreator(this).append("id", this.getId()) .append("new", this.isNew()) From 88b5933273ceb94bf95f4c406cab9d3b12501652 Mon Sep 17 00:00:00 2001 From: Shay Keren Date: Wed, 30 Jul 2025 11:48:17 +0300 Subject: [PATCH 2/2] feat: add efficient query methods with JOIN FETCH for pets --- .../samples/petclinic/owner/OwnerRepository.java | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) 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 f85b0ff44fb..9f04578cb6d 100644 --- a/src/main/java/org/springframework/samples/petclinic/owner/OwnerRepository.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/OwnerRepository.java @@ -13,9 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.samples.petclinic.owner; - -import java.util.List;import org.springframework.data.domain.Page; +package org.springframework.samples.petclinic.owner;import java.util.List;import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.Repository; @@ -32,9 +30,7 @@ * @author Juergen Hoeller * @author Sam Brannen * @author Michael Isvy - */public interface OwnerRepository extends Repository { - - /** + */public interface OwnerRepository extends Repository {/** * Retrieve all {@link PetType}s from the data store. * @return a Collection of {@link PetType}s. */ @@ -52,9 +48,7 @@ @Query("SELECT DISTINCT owner FROM Owner owner left join fetch owner.pets WHERE owner.lastName LIKE :lastName% ") @Transactional(readOnly = true) - Page findByLastName(@Param("lastName") String lastName, Pageable pageable); - - /** + Page findByLastName(@Param("lastName") String lastName, Pageable pageable);/** * Retrieve all {@link Owner}s from the data store with their pets. * @return a Collection of {@link Owner}s. */ @@ -77,9 +71,7 @@ @Query("SELECT SIZE(o.pets) FROM Owner o WHERE o.id = :id") @Transactional(readOnly = true) - int countPets(int id); - - /** + int countPets(int id);/** * Returns all the owners from data store **/ @Query("SELECT owner FROM Owner owner left join fetch owner.pets")