Skip to content

Commit e316074

Browse files
committed
Set fetch size for Hibernate
Solves the N+1 problem for this size of dataset and the standard repository queries. Much simpler than messing with the queries or with the default fetch type. Fixes #2157
1 parent a4fcf04 commit e316074

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

src/main/resources/application.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ spring.thymeleaf.mode=HTML
1010
spring.jpa.hibernate.ddl-auto=none
1111
spring.jpa.open-in-view=false
1212
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategySnakeCaseImpl
13+
spring.jpa.properties.hibernate.default_batch_fetch_size=16
1314

1415
# Internationalization
1516
spring.messages.basename=messages/messages

src/test/java/org/springframework/samples/petclinic/PetClinicIntegrationTests.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import org.springframework.samples.petclinic.vet.VetRepository;
3232
import org.springframework.web.client.RestTemplate;
3333

34-
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
34+
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = "logging.level.sql=DEBUG")
3535
public class PetClinicIntegrationTests {
3636

3737
@LocalServerPort
@@ -56,6 +56,13 @@ void testOwnerDetails() {
5656
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
5757
}
5858

59+
@Test
60+
void testOwnerList() {
61+
RestTemplate template = builder.rootUri("http://localhost:" + port).build();
62+
ResponseEntity<String> result = template.exchange(RequestEntity.get("/owners?lastName=").build(), String.class);
63+
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
64+
}
65+
5966
public static void main(String[] args) {
6067
SpringApplication.run(PetClinicApplication.class, "--spring.docker.compose.lifecycle-management=NONE");
6168
}

0 commit comments

Comments
 (0)