diff --git a/src/main/java/org/springframework/samples/petclinic/vet/VetController.java b/src/main/java/org/springframework/samples/petclinic/vet/VetController.java index 3240814a6f1..a00cf002c9b 100644 --- a/src/main/java/org/springframework/samples/petclinic/vet/VetController.java +++ b/src/main/java/org/springframework/samples/petclinic/vet/VetController.java @@ -13,11 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.samples.petclinic.vet; - -import java.util.List; - -import org.springframework.data.domain.Page; +package org.springframework.samples.petclinic.vet;import java.util.List;import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Controller; @@ -32,16 +28,13 @@ * @author Ken Krebs * @author Arjen Poutsma */ -@Controller -class VetController { +@Controllerclass VetController { private final VetRepository vetRepository; public VetController(VetRepository clinicService) { this.vetRepository = clinicService; - } - - @GetMapping("/vets.html") + }@GetMapping("/vets.html") public String showVetList(@RequestParam(defaultValue = "1") int page, Model model) { // Here we are returning an object of type 'Vets' rather than a collection of Vet // objects so it is simpler for Object-Xml mapping @@ -58,21 +51,17 @@ private String addPaginationModel(int page, Page paginated, Model model) { model.addAttribute("totalItems", paginated.getTotalElements()); model.addAttribute("listVets", listVets); return "vets/vetList"; - } - - private Page findPaginated(int page) { + }private Page findPaginated(int page) { int pageSize = 5; Pageable pageable = PageRequest.of(page - 1, pageSize); - return vetRepository.findAll(pageable); - } - - @GetMapping({ "/vets" }) + return vetRepository.findAllWithSpecialties(pageable); + }@GetMapping({ "/vets" }) public @ResponseBody Vets showResourcesVetList() { // Here we are returning an object of type 'Vets' rather than a collection of Vet // objects so it is simpler for JSon/Object mapping Vets vets = new Vets(); - vets.getVetList().addAll(this.vetRepository.findAll()); + vets.getVetList().addAll(this.vetRepository.findAllWithSpecialties()); return vets; } -} +} \ 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..377c814254a 100644 --- a/src/main/java/org/springframework/samples/petclinic/vet/VetRepository.java +++ b/src/main/java/org/springframework/samples/petclinic/vet/VetRepository.java @@ -13,16 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.samples.petclinic.vet; - -import org.springframework.cache.annotation.Cacheable; +package org.springframework.samples.petclinic.vet;import org.springframework.cache.annotation.Cacheable; 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 org.springframework.transaction.annotation.Transactional;import java.util.Collection; /** * Repository class for Vet domain objects All method names are compliant @@ -34,8 +31,7 @@ * @author Juergen Hoeller * @author Sam Brannen * @author Michael Isvy - */ -public interface VetRepository extends Repository { + */import org.springframework.data.jpa.repository.Query;public interface VetRepository extends Repository { /** * Retrieve all Vets from the data store. @@ -55,4 +51,14 @@ public interface VetRepository extends Repository { @Cacheable("vets") Page findAll(Pageable pageable) throws DataAccessException; -} + @Query("SELECT v FROM Vet v LEFT JOIN FETCH v.specialties") + @Transactional(readOnly = true) + @Cacheable("vets") + List findAllWithSpecialties() throws DataAccessException; + + @Query("SELECT v FROM Vet v LEFT JOIN FETCH v.specialties") + @Transactional(readOnly = true) + @Cacheable("vets") + Page findAllWithSpecialties(Pageable pageable) throws DataAccessException; + +} \ No newline at end of file