From 5589088951aef483b291df214f82cde6a486bb1c Mon Sep 17 00:00:00 2001 From: Shay Keren Date: Thu, 24 Jul 2025 11:50:51 +0300 Subject: [PATCH 1/3] fix: Change specialties fetch type to LAZY to prevent N+1 queries --- .../java/org/springframework/samples/petclinic/vet/Vet.java | 4 ++-- 1 file changed, 2 insertions(+), 2 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..b0bd1de2fed 100644 --- a/src/main/java/org/springframework/samples/petclinic/vet/Vet.java +++ b/src/main/java/org/springframework/samples/petclinic/vet/Vet.java @@ -45,7 +45,7 @@ @Table(name = "vets") public class Vet extends Person { - @ManyToMany(fetch = FetchType.EAGER) + @ManyToMany(fetch = FetchType.LAZY) @JoinTable(name = "vet_specialties", joinColumns = @JoinColumn(name = "vet_id"), inverseJoinColumns = @JoinColumn(name = "specialty_id")) private Set specialties; @@ -76,4 +76,4 @@ public void addSpecialty(Specialty specialty) { getSpecialtiesInternal().add(specialty); } -} +} \ No newline at end of file From 4269eea0ec55a3106a26441a1f5902fea92b73a6 Mon Sep 17 00:00:00 2001 From: Shay Keren Date: Thu, 24 Jul 2025 11:51:07 +0300 Subject: [PATCH 2/3] fix: Add JOIN FETCH query and optimize caching for vets --- .../samples/petclinic/vet/VetRepository.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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..7d0bba88db2 100644 --- a/src/main/java/org/springframework/samples/petclinic/vet/VetRepository.java +++ b/src/main/java/org/springframework/samples/petclinic/vet/VetRepository.java @@ -19,6 +19,8 @@ import org.springframework.dao.DataAccessException; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; +import org.springframework.data.jpa.repository.EntityGraph; +import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.Repository; import org.springframework.transaction.annotation.Transactional; @@ -41,6 +43,7 @@ public interface VetRepository extends Repository { * Retrieve all Vets from the data store. * @return a Collection of Vets */ + @Query("SELECT DISTINCT vet FROM Vet vet LEFT JOIN FETCH vet.specialties") @Transactional(readOnly = true) @Cacheable("vets") Collection findAll() throws DataAccessException; @@ -51,8 +54,9 @@ public interface VetRepository extends Repository { * @return * @throws DataAccessException */ + @EntityGraph(attributePaths = {"specialties"}) @Transactional(readOnly = true) @Cacheable("vets") Page findAll(Pageable pageable) throws DataAccessException; -} +} \ No newline at end of file From 0422ccf7a62eba945bc1e6af352aca83bb6ffbc0 Mon Sep 17 00:00:00 2001 From: Shay Keren Date: Thu, 24 Jul 2025 11:51:42 +0300 Subject: [PATCH 3/3] fix: Add indexes on vet_specialties join table to improve query performance --- src/main/resources/db/mysql/schema.sql | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/resources/db/mysql/schema.sql b/src/main/resources/db/mysql/schema.sql index 2591a516dea..7916a17215e 100644 --- a/src/main/resources/db/mysql/schema.sql +++ b/src/main/resources/db/mysql/schema.sql @@ -16,7 +16,9 @@ CREATE TABLE IF NOT EXISTS vet_specialties ( specialty_id INT(4) UNSIGNED NOT NULL, FOREIGN KEY (vet_id) REFERENCES vets(id), FOREIGN KEY (specialty_id) REFERENCES specialties(id), - UNIQUE (vet_id,specialty_id) + UNIQUE (vet_id,specialty_id), + INDEX idx_vet_specialties_vet (vet_id), + INDEX idx_vet_specialties_specialty (specialty_id) ) engine=InnoDB; CREATE TABLE IF NOT EXISTS types ( @@ -52,4 +54,4 @@ CREATE TABLE IF NOT EXISTS visits ( visit_date DATE, description VARCHAR(255), FOREIGN KEY (pet_id) REFERENCES pets(id) -) engine=InnoDB; +) engine=InnoDB; \ No newline at end of file