From 44a2de8dd9f71ee51a1eb4def3478e44e80131db Mon Sep 17 00:00:00 2001 From: Shay Keren Date: Mon, 21 Jul 2025 13:38:56 +0300 Subject: [PATCH 1/2] fix: change specialties fetch type from EAGER to LAZY to prevent N+1 queries --- .../springframework/samples/petclinic/vet/Vet.java | 11 ++++------- 1 file changed, 4 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..8d39b16b3cc 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,9 @@ * @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) // Changed from EAGER to LAZY to prevent N+1 queries @JoinTable(name = "vet_specialties", joinColumns = @JoinColumn(name = "vet_id"), inverseJoinColumns = @JoinColumn(name = "specialty_id")) private Set specialties; @@ -76,4 +73,4 @@ public void addSpecialty(Specialty specialty) { getSpecialtiesInternal().add(specialty); } -} +} \ No newline at end of file From 5b916c7ea80b8a1eb40ffe214c1eda636ebd478f Mon Sep 17 00:00:00 2001 From: Shay Keren Date: Mon, 21 Jul 2025 13:39:44 +0300 Subject: [PATCH 2/2] feat: add eager loading for vet specialties using EntityGraph --- .../samples/petclinic/vet/VetRepository.java | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 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..54f22f48ad9 100644 --- a/src/main/java/org/springframework/samples/petclinic/vet/VetRepository.java +++ b/src/main/java/org/springframework/samples/petclinic/vet/VetRepository.java @@ -21,8 +21,11 @@ import org.springframework.data.domain.Pageable; import org.springframework.data.repository.Repository; import org.springframework.transaction.annotation.Transactional; - +import org.springframework.data.jpa.repository.EntityGraph; +import org.springframework.data.jpa.repository.Query; import java.util.Collection; +import org.springframework.data.jpa.repository.EntityGraph; +import org.springframework.data.jpa.repository.Query; /** * Repository class for Vet domain objects All method names are compliant @@ -34,7 +37,9 @@ * @author Juergen Hoeller * @author Sam Brannen * @author Michael Isvy - */ + */import org.springframework.data.jpa.repository.EntityGraph; +import org.springframework.data.jpa.repository.Query; + public interface VetRepository extends Repository { /** @@ -43,6 +48,7 @@ public interface VetRepository extends Repository { */ @Transactional(readOnly = true) @Cacheable("vets") + @EntityGraph(attributePaths = {"specialties"}) Collection findAll() throws DataAccessException; /** @@ -53,6 +59,11 @@ public interface VetRepository extends Repository { */ @Transactional(readOnly = true) @Cacheable("vets") + @EntityGraph(attributePaths = {"specialties"}) Page findAll(Pageable pageable) throws DataAccessException; -} + @EntityGraph(attributePaths = {"specialties"}) + @Query("SELECT DISTINCT vet FROM Vet vet LEFT JOIN FETCH vet.specialties") + Collection findAllWithSpecialties() throws DataAccessException; + +} \ No newline at end of file