Skip to content

Commit aca4597

Browse files
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.
1 parent aadaa6b commit aca4597

File tree

3 files changed

+11
-14
lines changed

3 files changed

+11
-14
lines changed

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -559,23 +559,22 @@ private CodeBlock applyEntityGraph(AotEntityGraph entityGraph, String queryVaria
559559

560560
CodeBlock.Builder builder = CodeBlock.builder();
561561

562+
String entityGraphVariable = context.localVariable("entityGraph");
562563
if (StringUtils.hasText(entityGraph.name())) {
563564

564565
builder.addStatement("$T<?> $L = $L.getEntityGraph($S)", jakarta.persistence.EntityGraph.class,
565-
context.localVariable("entityGraph"),
566-
context.fieldNameOf(EntityManager.class), entityGraph.name());
566+
entityGraphVariable, context.fieldNameOf(EntityManager.class), entityGraph.name());
567567
} else {
568568

569-
builder.addStatement("$T<$T> $L = $L.createEntityGraph($T.class)",
570-
jakarta.persistence.EntityGraph.class, context.getDomainType(),
571-
context.localVariable("entityGraph"),
572-
context.fieldNameOf(EntityManager.class), context.getDomainType());
569+
builder.addStatement("$T<$T> $L = $L.createEntityGraph($T.class)", jakarta.persistence.EntityGraph.class,
570+
context.getDomainType(), entityGraphVariable, context.fieldNameOf(EntityManager.class),
571+
context.getDomainType());
573572

574573
for (String attributePath : entityGraph.attributePaths()) {
575574

576575
String[] pathComponents = StringUtils.delimitedListToStringArray(attributePath, ".");
577576

578-
StringBuilder chain = new StringBuilder(context.localVariable("entityGraph"));
577+
StringBuilder chain = new StringBuilder(entityGraphVariable);
579578
for (int i = 0; i < pathComponents.length; i++) {
580579

581580
if (i < pathComponents.length - 1) {
@@ -587,11 +586,10 @@ private CodeBlock applyEntityGraph(AotEntityGraph entityGraph, String queryVaria
587586

588587
builder.addStatement(chain.toString(), (Object[]) pathComponents);
589588
}
590-
591-
builder.addStatement("$L.setHint($S, $L)", queryVariableName, entityGraph.type().getKey(),
592-
context.localVariable("entityGraph"));
593589
}
594590

591+
builder.addStatement("$L.setHint($S, $L)", queryVariableName, entityGraph.type().getKey(), entityGraphVariable);
592+
595593
return builder.build();
596594
}
597595

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
@@ -637,13 +637,12 @@ void shouldApplyQueryHints() {
637637
.withMessageContaining("No enum constant jakarta.persistence.CacheStoreMode.foo");
638638
}
639639

640-
@Test // GH-3830
640+
@Test // GH-3830, GH-4097
641641
void shouldApplyNamedEntityGraph() {
642642

643643
User chewie = fragment.findWithNamedEntityGraphByFirstname("Chewbacca");
644644

645-
assertThat(chewie.getManager()).isInstanceOf(HibernateProxy.class);
646-
assertThat(chewie.getRoles()).isNotInstanceOf(HibernateProxy.class);
645+
assertThat(chewie.getManager()).isNotInstanceOf(HibernateProxy.class);
647646
}
648647

649648
@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
@@ -246,7 +246,7 @@ List<User> findWithParameterNameByLastnameStartingWithOrLastnameEndingWith(@Para
246246
@QueryHints(value = { @QueryHint(name = "jakarta.persistence.cache.storeMode", value = "foo") }, forCounting = false)
247247
List<User> findHintedByLastname(String lastname);
248248

249-
@EntityGraph(type = EntityGraph.EntityGraphType.FETCH, value = "User.overview")
249+
@EntityGraph(type = EntityGraph.EntityGraphType.FETCH, value = "User.detail")
250250
User findWithNamedEntityGraphByFirstname(String firstname);
251251

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

0 commit comments

Comments
 (0)