mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2026-01-12 04:51:11 +00:00
Make Pet.visits @Transient
In that way the Pet and Visit entities are more modular, and we don't have to rely on a bit of a hack which is that Hibernate happily maps Pet to its ID in the entity generation (but fails if you do it at compile time).
This commit is contained in:
parent
d6bdc13bdb
commit
7ef045dac8
4 changed files with 77 additions and 25 deletions
|
|
@ -15,6 +15,7 @@
|
|||
*/
|
||||
package org.springframework.samples.petclinic.owner;
|
||||
|
||||
import org.springframework.samples.petclinic.visit.VisitRepository;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.validation.BindingResult;
|
||||
|
|
@ -40,10 +41,12 @@ class OwnerController {
|
|||
|
||||
private static final String VIEWS_OWNER_CREATE_OR_UPDATE_FORM = "owners/createOrUpdateOwnerForm";
|
||||
private final OwnerRepository owners;
|
||||
private VisitRepository visits;
|
||||
|
||||
|
||||
public OwnerController(OwnerRepository clinicService) {
|
||||
public OwnerController(OwnerRepository clinicService, VisitRepository visits) {
|
||||
this.owners = clinicService;
|
||||
this.visits = visits;
|
||||
}
|
||||
|
||||
@InitBinder
|
||||
|
|
@ -126,7 +129,11 @@ class OwnerController {
|
|||
@GetMapping("/owners/{ownerId}")
|
||||
public ModelAndView showOwner(@PathVariable("ownerId") int ownerId) {
|
||||
ModelAndView mav = new ModelAndView("owners/ownerDetails");
|
||||
mav.addObject(this.owners.findById(ownerId));
|
||||
Owner owner = this.owners.findById(ownerId);
|
||||
for (Pet pet : owner.getPets()) {
|
||||
pet.setVisitsInternal(visits.findByPetId(pet.getId()));
|
||||
}
|
||||
mav.addObject(owner);
|
||||
return mav;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -17,20 +17,19 @@ package org.springframework.samples.petclinic.owner;
|
|||
|
||||
import java.time.LocalDate;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Table;
|
||||
import javax.persistence.Transient;
|
||||
|
||||
import org.springframework.beans.support.MutableSortDefinition;
|
||||
import org.springframework.beans.support.PropertyComparator;
|
||||
|
|
@ -61,7 +60,7 @@ public class Pet extends NamedEntity {
|
|||
@JoinColumn(name = "owner_id")
|
||||
private Owner owner;
|
||||
|
||||
@OneToMany(cascade = CascadeType.ALL, mappedBy = "petId", fetch = FetchType.EAGER)
|
||||
@Transient
|
||||
private Set<Visit> visits = new LinkedHashSet<>();
|
||||
|
||||
public void setBirthDate(LocalDate birthDate) {
|
||||
|
|
@ -95,8 +94,8 @@ public class Pet extends NamedEntity {
|
|||
return this.visits;
|
||||
}
|
||||
|
||||
protected void setVisitsInternal(Set<Visit> visits) {
|
||||
this.visits = visits;
|
||||
protected void setVisitsInternal(Collection<Visit> visits) {
|
||||
this.visits = new LinkedHashSet<>(visits);
|
||||
}
|
||||
|
||||
public List<Visit> getVisits() {
|
||||
|
|
|
|||
|
|
@ -15,15 +15,20 @@
|
|||
*/
|
||||
package org.springframework.samples.petclinic.owner;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import javax.validation.Valid;
|
||||
|
||||
import org.springframework.samples.petclinic.visit.Visit;
|
||||
import org.springframework.samples.petclinic.visit.VisitRepository;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.bind.WebDataBinder;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import java.util.Map;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.InitBinder;
|
||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
|
|
@ -62,6 +67,7 @@ class VisitController {
|
|||
@ModelAttribute("visit")
|
||||
public Visit loadPetWithVisit(@PathVariable("petId") int petId, Map<String, Object> model) {
|
||||
Pet pet = this.pets.findById(petId);
|
||||
pet.setVisitsInternal(this.visits.findByPetId(petId));
|
||||
model.put("pet", pet);
|
||||
Visit visit = new Visit();
|
||||
pet.addVisit(visit);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue