Skip to content

Commit afff932

Browse files
christophstroblmp911de
authored andcommitted
Apply fetch graph by name in AOT repositories.
This commit fixes an issue where the entity graph was read but never applied to the query. Closes #4097 Original pull request: #4100
1 parent 7225547 commit afff932

File tree

3 files changed

+9
-10
lines changed

3 files changed

+9
-10
lines changed

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/aot/JpaCodeBlocks.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -551,21 +551,22 @@ private CodeBlock applyEntityGraph(AotEntityGraph entityGraph, String queryVaria
551551

552552
CodeBlock.Builder builder = CodeBlock.builder();
553553

554+
String entityGraphVariable = context.localVariable("entityGraph");
554555
if (StringUtils.hasText(entityGraph.name())) {
555556

556557
builder.addStatement("$T<?> $L = $L.getEntityGraph($S)", jakarta.persistence.EntityGraph.class,
557-
context.localVariable("entityGraph"), context.fieldNameOf(EntityManager.class), entityGraph.name());
558+
entityGraphVariable, context.fieldNameOf(EntityManager.class), entityGraph.name());
558559
} else {
559560

560561
builder.addStatement("$T<$T> $L = $L.createEntityGraph($T.class)", jakarta.persistence.EntityGraph.class,
561-
context.getDomainType(), context.localVariable("entityGraph"), context.fieldNameOf(EntityManager.class),
562+
context.getDomainType(), entityGraphVariable, context.fieldNameOf(EntityManager.class),
562563
context.getDomainType());
563564

564565
for (String attributePath : entityGraph.attributePaths()) {
565566

566567
String[] pathComponents = StringUtils.delimitedListToStringArray(attributePath, ".");
567568

568-
StringBuilder chain = new StringBuilder(context.localVariable("entityGraph"));
569+
StringBuilder chain = new StringBuilder(entityGraphVariable);
569570
for (int i = 0; i < pathComponents.length; i++) {
570571

571572
if (i < pathComponents.length - 1) {
@@ -577,11 +578,10 @@ private CodeBlock applyEntityGraph(AotEntityGraph entityGraph, String queryVaria
577578

578579
builder.addStatement(chain.toString(), (Object[]) pathComponents);
579580
}
580-
581-
builder.addStatement("$L.setHint($S, $L)", queryVariableName, entityGraph.type().getKey(),
582-
context.localVariable("entityGraph"));
583581
}
584582

583+
builder.addStatement("$L.setHint($S, $L)", queryVariableName, entityGraph.type().getKey(), entityGraphVariable);
584+
585585
return builder.build();
586586
}
587587

spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/aot/JpaRepositoryContributorIntegrationTests.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -662,13 +662,12 @@ void shouldApplyQueryHints() {
662662
.withMessageContaining("No enum constant jakarta.persistence.CacheStoreMode.foo");
663663
}
664664

665-
@Test // GH-3830
665+
@Test // GH-3830, GH-4097
666666
void shouldApplyNamedEntityGraph() {
667667

668668
User chewie = fragment.findWithNamedEntityGraphByFirstname("Chewbacca");
669669

670-
assertThat(chewie.getManager()).isInstanceOf(HibernateProxy.class);
671-
assertThat(chewie.getRoles()).isNotInstanceOf(HibernateProxy.class);
670+
assertThat(chewie.getManager()).isNotInstanceOf(HibernateProxy.class);
672671
}
673672

674673
@Test // GH-3830

spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/aot/UserRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ List<User> findWithParameterNameByLastnameStartingWithOrLastnameEndingWith(@Para
251251
@QueryHints(value = { @QueryHint(name = "jakarta.persistence.cache.storeMode", value = "foo") }, forCounting = false)
252252
List<User> findHintedByLastname(String lastname);
253253

254-
@EntityGraph(type = EntityGraph.EntityGraphType.FETCH, value = "User.overview")
254+
@EntityGraph(type = EntityGraph.EntityGraphType.FETCH, value = "User.detail")
255255
User findWithNamedEntityGraphByFirstname(String firstname);
256256

257257
@EntityGraph(type = EntityGraph.EntityGraphType.FETCH, attributePaths = { "roles", "manager.roles" })

0 commit comments

Comments
 (0)