Skip to content

Commit 30fee0f

Browse files
committed
fix: change pets fetch type to lazy and add batch size optimization
1 parent 30616c1 commit 30fee0f

1 file changed

Lines changed: 8 additions & 16 deletions

File tree

  • src/main/java/org/springframework/samples/petclinic/owner

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

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

2121
import org.springframework.core.style.ToStringCreator;
2222
import org.springframework.samples.petclinic.model.Person;
23-
import org.springframework.util.Assert;
24-
25-
import jakarta.persistence.CascadeType;
23+
import org.springframework.util.Assert;import jakarta.persistence.CascadeType;
2624
import jakarta.persistence.Column;
2725
import jakarta.persistence.Entity;
2826
import jakarta.persistence.FetchType;
@@ -43,8 +41,7 @@
4341
* @author Oliver Drotbohm
4442
*/
4543
@Entity
46-
@Table(name = "owners")
47-
public class Owner extends Person {
44+
@Table(name = "owners")public class Owner extends Person {
4845

4946
@Column(name = "address")
5047
@NotBlank
@@ -59,9 +56,10 @@ public class Owner extends Person {
5956
@Digits(fraction = 0, integer = 10)
6057
private String telephone;
6158

62-
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
59+
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
6360
@JoinColumn(name = "owner_id")
6461
@OrderBy("name")
62+
@BatchSize(size = 100)
6563
private List<Pet> pets = new ArrayList<>();
6664

6765
public String getAddress() {
@@ -96,9 +94,7 @@ public void addPet(Pet pet) {
9694
if (pet.isNew()) {
9795
getPets().add(pet);
9896
}
99-
}
100-
101-
/**
97+
}/**
10298
* Return the Pet with the given name, or null if none found for this Owner.
10399
* @param name to test
104100
* @return a pet if pet name is already in use
@@ -122,9 +118,7 @@ public Pet getPet(Integer id) {
122118
}
123119
}
124120
return null;
125-
}
126-
127-
/**
121+
}/**
128122
* Return the Pet with the given name, or null if none found for this Owner.
129123
* @param name to test
130124
* @return a pet if pet name is already in use
@@ -152,9 +146,7 @@ public String toString() {
152146
.append("city", this.city)
153147
.append("telephone", this.telephone)
154148
.toString();
155-
}
156-
157-
/**
149+
}/**
158150
* Adds the given {@link Visit} to the {@link Pet} with the given identifier.
159151
* @param petId the identifier of the {@link Pet}, must not be {@literal null}.
160152
* @param visit the visit to add, must not be {@literal null}.
@@ -171,4 +163,4 @@ public void addVisit(Integer petId, Visit visit) {
171163
pet.addVisit(visit);
172164
}
173165

174-
}
166+
}

0 commit comments

Comments
 (0)