Skip to content

Commit 62c6caf

Browse files
check attribute type for association
1 parent b47b8e2 commit 62c6caf

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/JpqlUtils.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,17 @@ public JpqlQueryBuilder.PathExpression toExpressionRecursively(Metamodel metamod
8989

9090
// except its a collection type on the root
9191
if (from instanceof EntityType<?> && property.isCollection()) {
92-
return new JpqlQueryBuilder.PathAndOrigin(property, source, false);
92+
Attribute<?, ?> nextAttribute = resolveAttribute(metamodel, from, property);
93+
if(nextAttribute != null && nextAttribute.isAssociation()) {
94+
return new JpqlQueryBuilder.PathAndOrigin(property, source, false);
95+
}
9396
}
9497
return new JpqlQueryBuilder.PathAndOrigin(property, joinSource, true);
9598
}
9699

97100
PropertyPath nextProperty = Objects.requireNonNull(property.next(), "An element of the property path is null");
98101

99-
ManagedType<?> managedTypeForModel = getManagedTypeForModel(from);
100-
Attribute<?, ?> nextAttribute = getModelForPath(metamodel, property, managedTypeForModel, from);
102+
Attribute<?, ?> nextAttribute = resolveAttribute(metamodel, from, property);
101103

102104
if (nextAttribute == null) {
103105
throw new IllegalStateException("Binding property is null");
@@ -112,6 +114,13 @@ public JpqlQueryBuilder.PathExpression toExpressionRecursively(Metamodel metamod
112114
requiresOuterJoin);
113115
}
114116

117+
private static @Nullable Attribute<?, ?> resolveAttribute(Metamodel metamodel, Bindable<?> from,
118+
PropertyPath property) {
119+
120+
ManagedType<?> managedType = getManagedTypeForModel(from);
121+
return getModelForPath(metamodel, property, managedType, from);
122+
}
123+
115124
private static @Nullable Attribute<?, ?> getModelForPath(@Nullable Metamodel metamodel, PropertyPath path,
116125
@Nullable ManagedType<?> managedType, @Nullable Bindable<?> fallback) {
117126

@@ -148,8 +157,7 @@ record BindablePathResolver(Metamodel metamodel,
148157
}
149158

150159
private @Nullable Attribute<?, ?> resolveAttribute(PropertyPath propertyPath) {
151-
ManagedType<?> managedType = getManagedTypeForModel(bindable);
152-
return getModelForPath(metamodel, propertyPath, managedType, bindable);
160+
return JpqlExpressionFactory.resolveAttribute(metamodel, bindable, propertyPath);
153161
}
154162

155163
@Override

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,8 @@ void executesInKeywordForPageCorrectly() {}
4040
@Override
4141
void shouldProjectWithKeysetScrolling() {}
4242

43+
@Disabled
44+
@Override
45+
void executesQueryWithContainingOnCollectionViaJoin() {}
46+
4347
}

0 commit comments

Comments
 (0)