From ac2a8faf614e8f74af12b2f252a479c7efa2693d Mon Sep 17 00:00:00 2001 From: Shay Keren Date: Wed, 23 Jul 2025 15:15:41 +0300 Subject: [PATCH 1/3] fix: optimize entity relationships with lazy loading and batch size config --- .../springframework/samples/petclinic/vet/Vet.java | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/springframework/samples/petclinic/vet/Vet.java b/src/main/java/org/springframework/samples/petclinic/vet/Vet.java index 7a70155c3ea..2aa3a643f8f 100644 --- a/src/main/java/org/springframework/samples/petclinic/vet/Vet.java +++ b/src/main/java/org/springframework/samples/petclinic/vet/Vet.java @@ -23,9 +23,7 @@ import org.springframework.beans.support.MutableSortDefinition; import org.springframework.beans.support.PropertyComparator; -import org.springframework.samples.petclinic.model.Person; - -import jakarta.persistence.Entity; +import org.springframework.samples.petclinic.model.Person;import jakarta.persistence.Entity; import jakarta.persistence.FetchType; import jakarta.persistence.JoinColumn; import jakarta.persistence.JoinTable; @@ -42,10 +40,11 @@ * @author Arjen Poutsma */ @Entity -@Table(name = "vets") -public class Vet extends Person { +@Table(name = "vets")public class Vet extends Person { - @ManyToMany(fetch = FetchType.EAGER) + @ManyToMany(fetch = FetchType.LAZY) + @BatchSize(size = 100) + @ToString.Exclude @JoinTable(name = "vet_specialties", joinColumns = @JoinColumn(name = "vet_id"), inverseJoinColumns = @JoinColumn(name = "specialty_id")) private Set specialties; @@ -76,4 +75,4 @@ public void addSpecialty(Specialty specialty) { getSpecialtiesInternal().add(specialty); } -} +} \ No newline at end of file From 806533a640f5b08a2d40448f94dd5e99a1faf000 Mon Sep 17 00:00:00 2001 From: Shay Keren Date: Wed, 23 Jul 2025 15:16:00 +0300 Subject: [PATCH 2/3] feat add eager loading for specialties in findAll method --- .../samples/petclinic/vet/VetRepository.java | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/springframework/samples/petclinic/vet/VetRepository.java b/src/main/java/org/springframework/samples/petclinic/vet/VetRepository.java index 8b9e0823c86..0f20618c9a5 100644 --- a/src/main/java/org/springframework/samples/petclinic/vet/VetRepository.java +++ b/src/main/java/org/springframework/samples/petclinic/vet/VetRepository.java @@ -20,9 +20,7 @@ import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.repository.Repository; -import org.springframework.transaction.annotation.Transactional; - -import java.util.Collection; +import org.springframework.transaction.annotation.Transactional;import java.util.Collection; /** * Repository class for Vet domain objects All method names are compliant @@ -34,15 +32,16 @@ * @author Juergen Hoeller * @author Sam Brannen * @author Michael Isvy - */ -public interface VetRepository extends Repository { + */public interface VetRepository extends Repository { /** * Retrieve all Vets from the data store. * @return a Collection of Vets */ + @EntityGraph(attributePaths = {"specialties"}) @Transactional(readOnly = true) @Cacheable("vets") + @Query("SELECT DISTINCT vet FROM Vet vet JOIN FETCH vet.specialties") Collection findAll() throws DataAccessException; /** @@ -55,4 +54,4 @@ public interface VetRepository extends Repository { @Cacheable("vets") Page findAll(Pageable pageable) throws DataAccessException; -} +} \ No newline at end of file From e9696070cd2200a997ae79b1ca42dd0cb2008d4c Mon Sep 17 00:00:00 2001 From: Shay Keren Date: Wed, 23 Jul 2025 16:37:35 +0300 Subject: [PATCH 3/3] Add indexes for vet_specialties and specialties tables --- src/main/resources/db/postgres/indexes.sql | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 src/main/resources/db/postgres/indexes.sql diff --git a/src/main/resources/db/postgres/indexes.sql b/src/main/resources/db/postgres/indexes.sql new file mode 100644 index 00000000000..7b52df54ec2 --- /dev/null +++ b/src/main/resources/db/postgres/indexes.sql @@ -0,0 +1,3 @@ +-- Add indexes for vet_specialties and specialties tables +CREATE INDEX IF NOT EXISTS idx_vet_specialties_vet_id ON vet_specialties(vet_id); +CREATE INDEX IF NOT EXISTS idx_specialties_id ON specialties(id); \ No newline at end of file