Skip to content

Commit c953442

Browse files
Dave Syerdsyer
authored andcommitted
Remove PetRepository and use Owner as aggregate
Owner is really the aggregate root in DDD terms and there is no need to directly access the Pet entity.
1 parent 778161f commit c953442

File tree

21 files changed

+178
-202
lines changed

21 files changed

+178
-202
lines changed

pom.xml

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xmlns="http://maven.apache.org/POM/4.0.0"
4-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
53
<modelVersion>4.0.0</modelVersion>
64
<groupId>org.springframework.samples</groupId>
75
<artifactId>spring-petclinic</artifactId>
@@ -62,12 +60,12 @@
6260
<groupId>org.springframework.boot</groupId>
6361
<artifactId>spring-boot-starter-test</artifactId>
6462
<scope>test</scope>
65-
<exclusions>
66-
<exclusion>
67-
<groupId>org.junit.vintage</groupId>
68-
<artifactId>junit-vintage-engine</artifactId>
69-
</exclusion>
70-
</exclusions>
63+
<exclusions>
64+
<exclusion>
65+
<groupId>org.junit.vintage</groupId>
66+
<artifactId>junit-vintage-engine</artifactId>
67+
</exclusion>
68+
</exclusions>
7169
</dependency>
7270

7371
<!-- Databases - Uses H2 by default -->
@@ -317,20 +315,20 @@
317315
<artifactId>libsass-maven-plugin</artifactId>
318316
<version>0.2.26</version>
319317
<executions>
320-
<execution>
321-
<phase>generate-resources</phase>
322-
<?m2e execute onConfiguration,onIncremental?>
323-
<goals>
324-
<goal>compile</goal>
325-
</goals>
326-
</execution>
318+
<execution>
319+
<phase>generate-resources</phase>
320+
<?m2e execute onConfiguration,onIncremental?>
321+
<goals>
322+
<goal>compile</goal>
323+
</goals>
324+
</execution>
327325
</executions>
328326
<configuration>
329-
<inputPath>${basedir}/src/main/scss/</inputPath>
330-
<outputPath>${basedir}/src/main/resources/static/resources/css/</outputPath>
331-
<includePath>${project.build.directory}/webjars/META-INF/resources/webjars/bootstrap/${webjars-bootstrap.version}/scss/</includePath>
327+
<inputPath>${basedir}/src/main/scss/</inputPath>
328+
<outputPath>${basedir}/src/main/resources/static/resources/css/</outputPath>
329+
<includePath>${project.build.directory}/webjars/META-INF/resources/webjars/bootstrap/${webjars-bootstrap.version}/scss/</includePath>
332330
</configuration>
333-
</plugin>
331+
</plugin>
334332
</plugins>
335333
</build>
336334
</profile>
@@ -363,7 +361,7 @@
363361
</goals>
364362
</pluginExecutionFilter>
365363
<action>
366-
<ignore/>
364+
<ignore />
367365
</action>
368366
</pluginExecution>
369367
<pluginExecution>
@@ -376,7 +374,7 @@
376374
</goals>
377375
</pluginExecutionFilter>
378376
<action>
379-
<ignore/>
377+
<ignore />
380378
</action>
381379
</pluginExecution>
382380
<pluginExecution>
@@ -389,7 +387,7 @@
389387
</goals>
390388
</pluginExecutionFilter>
391389
<action>
392-
<ignore/>
390+
<ignore />
393391
</action>
394392
</pluginExecution>
395393
</pluginExecutions>

src/main/java/org/springframework/samples/petclinic/PetClinicApplication.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
* @author Dave Syer
2626
*
2727
*/
28-
@SpringBootApplication(proxyBeanMethods = false)
28+
@SpringBootApplication
2929
public class PetClinicApplication {
3030

3131
public static void main(String[] args) {

src/main/java/org/springframework/samples/petclinic/model/BaseEntity.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515
*/
1616
package org.springframework.samples.petclinic.model;
1717

18+
import java.io.Serializable;
19+
1820
import javax.persistence.GeneratedValue;
1921
import javax.persistence.GenerationType;
2022
import javax.persistence.Id;
2123
import javax.persistence.MappedSuperclass;
22-
import java.io.Serializable;
2324

2425
/**
2526
* Simple JavaBean domain object with an id property. Used as a base class for objects

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

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public class Owner extends Person {
6060
@Digits(fraction = 0, integer = 10)
6161
private String telephone;
6262

63-
@OneToMany(cascade = CascadeType.ALL, mappedBy = "owner", fetch = FetchType.EAGER)
63+
@OneToMany(cascade = CascadeType.ALL, mappedBy = "ownerId", fetch = FetchType.EAGER)
6464
private Set<Pet> pets;
6565

6666
public String getAddress() {
@@ -108,7 +108,7 @@ public void addPet(Pet pet) {
108108
if (pet.isNew()) {
109109
getPetsInternal().add(pet);
110110
}
111-
pet.setOwner(this);
111+
pet.setOwnerId(getId());
112112
}
113113

114114
/**
@@ -120,6 +120,23 @@ public Pet getPet(String name) {
120120
return getPet(name, false);
121121
}
122122

123+
/**
124+
* Return the Pet with the given id, or null if none found for this Owner.
125+
* @param name to test
126+
* @return a pet if pet id is already in use
127+
*/
128+
public Pet getPet(Integer id) {
129+
for (Pet pet : getPetsInternal()) {
130+
if (!pet.isNew()) {
131+
Integer compId = pet.getId();
132+
if (compId.equals(id)) {
133+
return pet;
134+
}
135+
}
136+
}
137+
return null;
138+
}
139+
123140
/**
124141
* Return the Pet with the given name, or null if none found for this Owner.
125142
* @param name to test
@@ -130,7 +147,7 @@ public Pet getPet(String name, boolean ignoreNew) {
130147
for (Pet pet : getPetsInternal()) {
131148
if (!ignoreNew || !pet.isNew()) {
132149
String compName = pet.getName();
133-
compName = compName.toLowerCase();
150+
compName = compName == null ? "" : compName.toLowerCase();
134151
if (compName.equals(name)) {
135152
return pet;
136153
}
@@ -141,11 +158,10 @@ public Pet getPet(String name, boolean ignoreNew) {
141158

142159
@Override
143160
public String toString() {
144-
return new ToStringCreator(this)
145-
146-
.append("id", this.getId()).append("new", this.isNew()).append("lastName", this.getLastName())
147-
.append("firstName", this.getFirstName()).append("address", this.address).append("city", this.city)
148-
.append("telephone", this.telephone).toString();
161+
return new ToStringCreator(this).append("id", this.getId()).append("new", this.isNew())
162+
.append("lastName", this.getLastName()).append("firstName", this.getFirstName())
163+
.append("address", this.address).append("city", this.city).append("telephone", this.telephone)
164+
.toString();
149165
}
150166

151167
}

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

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

18+
import java.util.List;
19+
1820
import org.springframework.data.domain.Page;
1921
import org.springframework.data.domain.Pageable;
2022
import org.springframework.data.jpa.repository.Query;
@@ -35,6 +37,14 @@
3537
*/
3638
public interface OwnerRepository extends Repository<Owner, Integer> {
3739

40+
/**
41+
* Retrieve all {@link PetType}s from the data store.
42+
* @return a Collection of {@link PetType}s.
43+
*/
44+
@Query("SELECT ptype FROM PetType ptype ORDER BY ptype.name")
45+
@Transactional(readOnly = true)
46+
List<PetType> findPetTypes();
47+
3848
/**
3949
* Retrieve {@link Owner}s from the data store by last name, returning all owners
4050
* whose last name <i>starts</i> with the given name.

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

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,16 @@ public class Pet extends NamedEntity {
5656
@JoinColumn(name = "type_id")
5757
private PetType type;
5858

59-
@ManyToOne
60-
@JoinColumn(name = "owner_id")
61-
private Owner owner;
59+
@Column
60+
private Integer ownerId;
6261

6362
@Transient
6463
private Set<Visit> visits = new LinkedHashSet<>();
6564

65+
public void setOwnerId(Integer ownerId) {
66+
this.ownerId = ownerId;
67+
}
68+
6669
public void setBirthDate(LocalDate birthDate) {
6770
this.birthDate = birthDate;
6871
}
@@ -79,14 +82,6 @@ public void setType(PetType type) {
7982
this.type = type;
8083
}
8184

82-
public Owner getOwner() {
83-
return this.owner;
84-
}
85-
86-
protected void setOwner(Owner owner) {
87-
this.owner = owner;
88-
}
89-
9085
protected Set<Visit> getVisitsInternal() {
9186
if (this.visits == null) {
9287
this.visits = new HashSet<>();

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,15 @@ class PetController {
3636

3737
private static final String VIEWS_PETS_CREATE_OR_UPDATE_FORM = "pets/createOrUpdatePetForm";
3838

39-
private final PetRepository pets;
40-
4139
private final OwnerRepository owners;
4240

43-
public PetController(PetRepository pets, OwnerRepository owners) {
44-
this.pets = pets;
41+
public PetController(OwnerRepository owners) {
4542
this.owners = owners;
4643
}
4744

4845
@ModelAttribute("types")
4946
public Collection<PetType> populatePetTypes() {
50-
return this.pets.findPetTypes();
47+
return this.owners.findPetTypes();
5148
}
5249

5350
@ModelAttribute("owner")
@@ -84,28 +81,27 @@ public String processCreationForm(Owner owner, @Valid Pet pet, BindingResult res
8481
return VIEWS_PETS_CREATE_OR_UPDATE_FORM;
8582
}
8683
else {
87-
this.pets.save(pet);
84+
this.owners.save(owner);
8885
return "redirect:/owners/{ownerId}";
8986
}
9087
}
9188

9289
@GetMapping("/pets/{petId}/edit")
93-
public String initUpdateForm(@PathVariable("petId") int petId, ModelMap model) {
94-
Pet pet = this.pets.findById(petId);
90+
public String initUpdateForm(Owner owner, @PathVariable("petId") int petId, ModelMap model) {
91+
Pet pet = owner.getPet(petId);
9592
model.put("pet", pet);
9693
return VIEWS_PETS_CREATE_OR_UPDATE_FORM;
9794
}
9895

9996
@PostMapping("/pets/{petId}/edit")
10097
public String processUpdateForm(@Valid Pet pet, BindingResult result, Owner owner, ModelMap model) {
10198
if (result.hasErrors()) {
102-
pet.setOwner(owner);
10399
model.put("pet", pet);
104100
return VIEWS_PETS_CREATE_OR_UPDATE_FORM;
105101
}
106102
else {
107103
owner.addPet(pet);
108-
this.pets.save(pet);
104+
this.owners.save(owner);
109105
return "redirect:/owners/{ownerId}";
110106
}
111107
}

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

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

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

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

18-
import org.springframework.samples.petclinic.model.NamedEntity;
19-
2018
import javax.persistence.Entity;
2119
import javax.persistence.Table;
2220

21+
import org.springframework.samples.petclinic.model.NamedEntity;
22+
2323
/**
2424
* @author Juergen Hoeller Can be Cat, Dog, Hamster...
2525
*/

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@
3636
@Component
3737
public class PetTypeFormatter implements Formatter<PetType> {
3838

39-
private final PetRepository pets;
39+
private final OwnerRepository owners;
4040

4141
@Autowired
42-
public PetTypeFormatter(PetRepository pets) {
43-
this.pets = pets;
42+
public PetTypeFormatter(OwnerRepository owners) {
43+
this.owners = owners;
4444
}
4545

4646
@Override
@@ -50,7 +50,7 @@ public String print(PetType petType, Locale locale) {
5050

5151
@Override
5252
public PetType parse(String text, Locale locale) throws ParseException {
53-
Collection<PetType> findPetTypes = this.pets.findPetTypes();
53+
Collection<PetType> findPetTypes = this.owners.findPetTypes();
5454
for (PetType type : findPetTypes) {
5555
if (type.getName().equals(text)) {
5656
return type;

0 commit comments

Comments
 (0)