Skip to content

Commit 58fe629

Browse files
Dave Syerdsyer
authored andcommitted
Also remove VisitRepository
Rely on Hibernate to do lazy loading on visits.
1 parent a5da14a commit 58fe629

File tree

8 files changed

+43
-113
lines changed

8 files changed

+43
-113
lines changed

src/main/java/org/springframework/samples/petclinic/owner/OwnerController.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import org.springframework.data.domain.Page;
2424
import org.springframework.data.domain.PageRequest;
2525
import org.springframework.data.domain.Pageable;
26-
import org.springframework.samples.petclinic.visit.VisitRepository;
2726
import org.springframework.stereotype.Controller;
2827
import org.springframework.ui.Model;
2928
import org.springframework.validation.BindingResult;
@@ -48,11 +47,8 @@ class OwnerController {
4847

4948
private final OwnerRepository owners;
5049

51-
private final VisitRepository visits;
52-
53-
public OwnerController(OwnerRepository clinicService, VisitRepository visits) {
50+
public OwnerController(OwnerRepository clinicService) {
5451
this.owners = clinicService;
55-
this.visits = visits;
5652
}
5753

5854
@InitBinder
@@ -161,7 +157,7 @@ public ModelAndView showOwner(@PathVariable("ownerId") int ownerId) {
161157
ModelAndView mav = new ModelAndView("owners/ownerDetails");
162158
Owner owner = this.owners.findById(ownerId);
163159
for (Pet pet : owner.getPets()) {
164-
pet.setVisitsInternal(visits.findByPetId(pet.getId()));
160+
pet.getVisits();
165161
}
166162
mav.addObject(owner);
167163
return mav;

src/main/java/org/springframework/samples/petclinic/owner/Pet.java

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,20 @@
1616
package org.springframework.samples.petclinic.owner;
1717

1818
import java.time.LocalDate;
19-
import java.util.ArrayList;
2019
import java.util.Collection;
21-
import java.util.Collections;
22-
import java.util.HashSet;
2320
import java.util.LinkedHashSet;
24-
import java.util.List;
2521
import java.util.Set;
2622

23+
import javax.persistence.CascadeType;
2724
import javax.persistence.Column;
2825
import javax.persistence.Entity;
26+
import javax.persistence.FetchType;
2927
import javax.persistence.JoinColumn;
3028
import javax.persistence.ManyToOne;
29+
import javax.persistence.OneToMany;
30+
import javax.persistence.OrderBy;
3131
import javax.persistence.Table;
32-
import javax.persistence.Transient;
3332

34-
import org.springframework.beans.support.MutableSortDefinition;
35-
import org.springframework.beans.support.PropertyComparator;
3633
import org.springframework.format.annotation.DateTimeFormat;
3734
import org.springframework.samples.petclinic.model.NamedEntity;
3835
import org.springframework.samples.petclinic.visit.Visit;
@@ -59,7 +56,8 @@ public class Pet extends NamedEntity {
5956
@Column
6057
private Integer ownerId;
6158

62-
@Transient
59+
@OneToMany(cascade = CascadeType.ALL, mappedBy = "petId", fetch = FetchType.LAZY)
60+
@OrderBy("visit_date ASC")
6361
private Set<Visit> visits = new LinkedHashSet<>();
6462

6563
public void setOwnerId(Integer ownerId) {
@@ -82,25 +80,12 @@ public void setType(PetType type) {
8280
this.type = type;
8381
}
8482

85-
protected Set<Visit> getVisitsInternal() {
86-
if (this.visits == null) {
87-
this.visits = new HashSet<>();
88-
}
83+
public Collection<Visit> getVisits() {
8984
return this.visits;
9085
}
9186

92-
protected void setVisitsInternal(Collection<Visit> visits) {
93-
this.visits = new LinkedHashSet<>(visits);
94-
}
95-
96-
public List<Visit> getVisits() {
97-
List<Visit> sortedVisits = new ArrayList<>(getVisitsInternal());
98-
PropertyComparator.sort(sortedVisits, new MutableSortDefinition("date", false, false));
99-
return Collections.unmodifiableList(sortedVisits);
100-
}
101-
10287
public void addVisit(Visit visit) {
103-
getVisitsInternal().add(visit);
88+
getVisits().add(visit);
10489
visit.setPetId(this.getId());
10590
}
10691

src/main/java/org/springframework/samples/petclinic/visit/Visit.java renamed to src/main/java/org/springframework/samples/petclinic/owner/Visit.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ public class Visit extends BaseEntity {
4040
private LocalDate date;
4141

4242
@NotEmpty
43-
@Column(name = "description")
43+
@Column
4444
private String description;
4545

46-
@Column(name = "pet_id")
46+
@Column
4747
private Integer petId;
4848

4949
/**

src/main/java/org/springframework/samples/petclinic/owner/VisitController.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,19 @@
1515
*/
1616
package org.springframework.samples.petclinic.owner;
1717

18+
import java.util.Map;
19+
20+
import javax.validation.Valid;
21+
1822
import org.springframework.samples.petclinic.visit.Visit;
19-
import org.springframework.samples.petclinic.visit.VisitRepository;
2023
import org.springframework.stereotype.Controller;
2124
import org.springframework.validation.BindingResult;
2225
import org.springframework.web.bind.WebDataBinder;
23-
import org.springframework.web.bind.annotation.*;
24-
25-
import javax.validation.Valid;
26-
import java.util.Map;
26+
import org.springframework.web.bind.annotation.GetMapping;
27+
import org.springframework.web.bind.annotation.InitBinder;
28+
import org.springframework.web.bind.annotation.ModelAttribute;
29+
import org.springframework.web.bind.annotation.PathVariable;
30+
import org.springframework.web.bind.annotation.PostMapping;
2731

2832
/**
2933
* @author Juergen Hoeller
@@ -35,12 +39,9 @@
3539
@Controller
3640
class VisitController {
3741

38-
private final VisitRepository visits;
39-
4042
private final OwnerRepository owners;
4143

42-
public VisitController(VisitRepository visits, OwnerRepository owners) {
43-
this.visits = visits;
44+
public VisitController(OwnerRepository owners) {
4445
this.owners = owners;
4546
}
4647

@@ -61,7 +62,6 @@ public Visit loadPetWithVisit(@PathVariable("ownerId") int ownerId, @PathVariabl
6162
Map<String, Object> model) {
6263
Owner owner = this.owners.findById(ownerId);
6364
Pet pet = owner.getPet(petId);
64-
pet.setVisitsInternal(this.visits.findByPetId(petId));
6565
model.put("pet", pet);
6666
Visit visit = new Visit();
6767
pet.addVisit(visit);
@@ -78,12 +78,14 @@ public String initNewVisitForm(@PathVariable("petId") int petId, Map<String, Obj
7878
// Spring MVC calls method loadPetWithVisit(...) before processNewVisitForm is
7979
// called
8080
@PostMapping("/owners/{ownerId}/pets/{petId}/visits/new")
81-
public String processNewVisitForm(@Valid Visit visit, BindingResult result) {
81+
public String processNewVisitForm(@PathVariable("ownerId") int ownerId, @Valid Visit visit, BindingResult result) {
8282
if (result.hasErrors()) {
8383
return "pets/createOrUpdateVisitForm";
8484
}
8585
else {
86-
this.visits.save(visit);
86+
Owner owner = this.owners.findById(ownerId);
87+
owner.getPet(visit.getPetId()).addVisit(visit);
88+
this.owners.save(owner);
8789
return "redirect:/owners/{ownerId}";
8890
}
8991
}

src/main/java/org/springframework/samples/petclinic/visit/VisitRepository.java

Lines changed: 0 additions & 46 deletions
This file was deleted.

src/test/java/org/springframework/samples/petclinic/owner/OwnerControllerTests.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
import org.springframework.data.domain.PageImpl;
4848
import org.springframework.data.domain.Pageable;
4949
import org.springframework.samples.petclinic.visit.Visit;
50-
import org.springframework.samples.petclinic.visit.VisitRepository;
5150
import org.springframework.test.web.servlet.MockMvc;
5251

5352
/**
@@ -66,9 +65,6 @@ class OwnerControllerTests {
6665
@MockBean
6766
private OwnerRepository owners;
6867

69-
@MockBean
70-
private VisitRepository visits;
71-
7268
private Owner george() {
7369
Owner george = new Owner();
7470
george.setId(TEST_OWNER_ID);
@@ -91,15 +87,17 @@ private Owner george() {
9187
@BeforeEach
9288
void setup() {
9389

90+
Owner george = george();
9491
given(this.owners.findByLastName(eq("Franklin"), any(Pageable.class)))
95-
.willReturn(new PageImpl<Owner>(Lists.newArrayList(george())));
92+
.willReturn(new PageImpl<Owner>(Lists.newArrayList(george)));
9693

97-
given(this.owners.findAll(any(Pageable.class))).willReturn(new PageImpl<Owner>(Lists.newArrayList(george())));
94+
given(this.owners.findAll(any(Pageable.class))).willReturn(new PageImpl<Owner>(Lists.newArrayList(george)));
9895

99-
given(this.owners.findById(TEST_OWNER_ID)).willReturn(george());
96+
given(this.owners.findById(TEST_OWNER_ID)).willReturn(george);
10097
Visit visit = new Visit();
10198
visit.setDate(LocalDate.now());
102-
given(this.visits.findByPetId(george().getPet("Max").getId())).willReturn(Collections.singletonList(visit));
99+
visit.setPetId(george.getPet("Max").getId());
100+
george.getPet("Max").getVisits().add(visit);
103101

104102
}
105103

src/test/java/org/springframework/samples/petclinic/owner/VisitControllerTests.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,20 @@
1616

1717
package org.springframework.samples.petclinic.owner;
1818

19+
import static org.mockito.BDDMockito.given;
20+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
21+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
22+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
23+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
24+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
25+
1926
import org.junit.jupiter.api.BeforeEach;
2027
import org.junit.jupiter.api.Test;
2128
import org.springframework.beans.factory.annotation.Autowired;
2229
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
2330
import org.springframework.boot.test.mock.mockito.MockBean;
24-
import org.springframework.samples.petclinic.visit.VisitRepository;
2531
import org.springframework.test.web.servlet.MockMvc;
2632

27-
import static org.mockito.BDDMockito.given;
28-
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
29-
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
30-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
31-
3233
/**
3334
* Test class for {@link VisitController}
3435
*
@@ -44,9 +45,6 @@ class VisitControllerTests {
4445
@Autowired
4546
private MockMvc mockMvc;
4647

47-
@MockBean
48-
private VisitRepository visits;
49-
5048
@MockBean
5149
private OwnerRepository owners;
5250

src/test/java/org/springframework/samples/petclinic/service/ClinicServiceTests.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import org.springframework.samples.petclinic.vet.Vet;
3737
import org.springframework.samples.petclinic.vet.VetRepository;
3838
import org.springframework.samples.petclinic.visit.Visit;
39-
import org.springframework.samples.petclinic.visit.VisitRepository;
4039
import org.springframework.stereotype.Service;
4140
import org.springframework.transaction.annotation.Transactional;
4241

@@ -76,9 +75,6 @@ class ClinicServiceTests {
7675
@Autowired
7776
protected OwnerRepository owners;
7877

79-
@Autowired
80-
protected VisitRepository visits;
81-
8278
@Autowired
8379
protected VetRepository vets;
8480

@@ -205,17 +201,18 @@ void shouldAddNewVisitForPet() {
205201
Visit visit = new Visit();
206202
pet7.addVisit(visit);
207203
visit.setDescription("test");
208-
this.visits.save(visit);
209204
this.owners.save(owner6);
210205

211206
owner6 = this.owners.findById(6);
212207
assertThat(pet7.getVisits().size()).isEqualTo(found + 1);
213-
assertThat(visit.getId()).isNotNull();
208+
assertThat(pet7.getVisits()).allMatch(value -> value.getId() != null);
214209
}
215210

216211
@Test
217212
void shouldFindVisitsByPetId() throws Exception {
218-
Collection<Visit> visits = this.visits.findByPetId(7);
213+
Owner owner6 = this.owners.findById(6);
214+
Pet pet7 = owner6.getPet(7);
215+
Collection<Visit> visits = pet7.getVisits();
219216
assertThat(visits).hasSize(2);
220217
Visit[] visitArr = visits.toArray(new Visit[visits.size()]);
221218
assertThat(visitArr[0].getDate()).isNotNull();

0 commit comments

Comments
 (0)