From de49be7d61123276bb40b333391f111ba5502753 Mon Sep 17 00:00:00 2001 From: Shay Keren Date: Thu, 24 Jul 2025 11:59:56 +0300 Subject: [PATCH 1/2] feat: optimize vet repository with fetch join queries --- .../samples/petclinic/vet/VetRepository.java | 70 +++++++++++++------ 1 file changed, 47 insertions(+), 23 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..8dac47d1e64 100644 --- a/src/main/java/org/springframework/samples/petclinic/vet/VetRepository.java +++ b/src/main/java/org/springframework/samples/petclinic/vet/VetRepository.java @@ -19,10 +19,11 @@ import org.springframework.dao.DataAccessException; 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; import org.springframework.transaction.annotation.Transactional; -import java.util.Collection; +import java.util.Collection;import java.util.Collection; /** * Repository class for Vet domain objects All method names are compliant @@ -34,25 +35,48 @@ * @author Juergen Hoeller * @author Sam Brannen * @author Michael Isvy - */ -public interface VetRepository extends Repository { - - /** - * Retrieve all Vets from the data store. - * @return a Collection of Vets - */ - @Transactional(readOnly = true) - @Cacheable("vets") - Collection findAll() throws DataAccessException; - - /** - * Retrieve all Vets from data store in Pages - * @param pageable - * @return - * @throws DataAccessException - */ - @Transactional(readOnly = true) - @Cacheable("vets") - Page findAll(Pageable pageable) throws DataAccessException; - -} + */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; + + /** + * Retrieve all Vets with specialties 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 findAllWithSpecialties() throws DataAccessException; + + /** + * Retrieve all Vets from data store in Pages + * @param pageable + * @return + * @throws DataAccessException + */ + @Query(value = "SELECT DISTINCT vet FROM Vet vet LEFT JOIN FETCH vet.specialties", + countQuery = "SELECT COUNT(DISTINCT vet) FROM Vet vet") + @Transactional(readOnly = true) + @Cacheable("vets") + Page findAll(Pageable pageable) throws DataAccessException; + + /** + * Retrieve all Vets with specialties from data store in Pages + * @param pageable + * @return + * @throws DataAccessException + */ + @Query(value = "SELECT DISTINCT vet FROM Vet vet LEFT JOIN FETCH vet.specialties", + countQuery = "SELECT COUNT(DISTINCT vet) FROM Vet vet") + @Transactional(readOnly = true) + @Cacheable("vets") + Page findAllWithSpecialties(Pageable pageable) throws DataAccessException; + +} \ No newline at end of file From 9da53021b2fe1eb17d102b1060e1a5b8d0b0a8bd Mon Sep 17 00:00:00 2001 From: Shay Keren Date: Thu, 24 Jul 2025 12:00:27 +0300 Subject: [PATCH 2/2] perf: optimize Vet entity with lazy loading and batch size for specialties --- .../springframework/samples/petclinic/vet/Vet.java | 12 +++++------- 1 file changed, 5 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..3e8b440ecee 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,10 @@ * @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) @JoinTable(name = "vet_specialties", joinColumns = @JoinColumn(name = "vet_id"), inverseJoinColumns = @JoinColumn(name = "specialty_id")) private Set specialties; @@ -76,4 +74,4 @@ public void addSpecialty(Specialty specialty) { getSpecialtiesInternal().add(specialty); } -} +} \ No newline at end of file