Alternative approach with eager fetch

This commit is contained in:
Dave Syer 2022-01-10 08:21:14 +00:00
parent e765e3ffe1
commit bdcaa85460
2 changed files with 1 additions and 6 deletions

View file

@ -18,7 +18,6 @@ package org.springframework.samples.petclinic.owner;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import javax.transaction.Transactional;
import javax.validation.Valid; import javax.validation.Valid;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
@ -154,13 +153,9 @@ class OwnerController {
* @return a ModelMap with the model attributes for the view * @return a ModelMap with the model attributes for the view
*/ */
@GetMapping("/owners/{ownerId}") @GetMapping("/owners/{ownerId}")
@Transactional
public ModelAndView showOwner(@PathVariable("ownerId") int ownerId) { public ModelAndView showOwner(@PathVariable("ownerId") int ownerId) {
ModelAndView mav = new ModelAndView("owners/ownerDetails"); ModelAndView mav = new ModelAndView("owners/ownerDetails");
Owner owner = this.owners.findById(ownerId); Owner owner = this.owners.findById(ownerId);
for (Pet pet : owner.getPets()) {
pet.getVisits().size();
}
mav.addObject(owner); mav.addObject(owner);
return mav; return mav;
} }

View file

@ -52,7 +52,7 @@ public class Pet extends NamedEntity {
@JoinColumn(name = "type_id") @JoinColumn(name = "type_id")
private PetType type; private PetType type;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY) @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "pet_id") @JoinColumn(name = "pet_id")
@OrderBy("visit_date ASC") @OrderBy("visit_date ASC")
private Set<Visit> visits = new LinkedHashSet<>(); private Set<Visit> visits = new LinkedHashSet<>();