Skip to content

Commit aedafa2

Browse files
committed
Include intermediate joins.
When fields from referenced entities got requested, ther intermediate necessary joins were not included.
1 parent 80c113a commit aedafa2

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/SqlGenerator.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -572,11 +572,7 @@ private Projection getProjection(Collection<SqlIdentifier> keyColumns, Query que
572572

573573
private void includeColumnAndJoin(AggregatePath aggregatePath, Set<Join> joins, Set<Expression> columns) {
574574

575-
// add a join if necessary
576-
Join join = getJoin(aggregatePath);
577-
if (join != null) {
578-
joins.add(join);
579-
}
575+
joins.addAll(getJoins(aggregatePath));
580576

581577
Column column = getColumn(aggregatePath);
582578
if (column != null) {
@@ -653,9 +649,24 @@ Column getColumn(AggregatePath path) {
653649
return sqlContext.getColumn(path);
654650
}
655651

652+
List<Join> getJoins(AggregatePath path) {
653+
654+
List<Join> joins = new ArrayList<>();
655+
while (!path.isRoot()) {
656+
Join join = getJoin(path);
657+
if (join != null) {
658+
joins.add(join);
659+
}
660+
661+
path = path.getParentPath();
662+
}
663+
return joins;
664+
}
665+
656666
@Nullable
657667
Join getJoin(AggregatePath path) {
658668

669+
// TODO: This doesn't handle paths with length > 1 correctly
659670
if (!path.isEntity() || path.isEmbedded() || path.isMultiValued()) {
660671
return null;
661672
}

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/convert/SqlGeneratorUnitTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ void selectByQueryWithMappedColumnPathsRendersCorrectSelection() {
400400

401401
assertThat(sql).contains( //
402402
"SELECT", //
403-
"ref.id1 AS id1, ref.content AS x_content", //
403+
"ref.x_content AS ref_x_content", //
404404
"FROM dummy_entity", //
405405
"LEFT OUTER JOIN referenced_entity ref ON ref.dummy_entity = dummy_entity.id1");
406406
}

0 commit comments

Comments
 (0)