Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -58,21 +51,17 @@ private String addPaginationModel(int page, Page<Vet> paginated, Model model) {
model.addAttribute("totalItems", paginated.getTotalElements());
model.addAttribute("listVets", listVets);
return "vets/vetList";
}

private Page<Vet> findPaginated(int page) {
}private Page<Vet> 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;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 <code>Vet</code> domain objects All method names are compliant
Expand All @@ -34,8 +31,7 @@
* @author Juergen Hoeller
* @author Sam Brannen
* @author Michael Isvy
*/
public interface VetRepository extends Repository<Vet, Integer> {
*/import org.springframework.data.jpa.repository.Query;public interface VetRepository extends Repository<Vet, Integer> {

/**
* Retrieve all <code>Vet</code>s from the data store.
Expand All @@ -55,4 +51,14 @@ public interface VetRepository extends Repository<Vet, Integer> {
@Cacheable("vets")
Page<Vet> findAll(Pageable pageable) throws DataAccessException;

}
@Query("SELECT v FROM Vet v LEFT JOIN FETCH v.specialties")
@Transactional(readOnly = true)
@Cacheable("vets")
List<Vet> findAllWithSpecialties() throws DataAccessException;

@Query("SELECT v FROM Vet v LEFT JOIN FETCH v.specialties")
@Transactional(readOnly = true)
@Cacheable("vets")
Page<Vet> findAllWithSpecialties(Pageable pageable) throws DataAccessException;

}