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 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