Skip to content

Commit 2326e23

Browse files
committed
Consider query columns as property names.
Query columns now get checked against property names. If such a property name is found, the property is used. Otherwise the column is considered a literal column and used as is in the SQL generation. Original pull request #1967 See #1803
1 parent 9c2a954 commit 2326e23

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

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

+27-12
Original file line numberDiff line numberDiff line change
@@ -541,24 +541,25 @@ private Projection getProjection(Collection<SqlIdentifier> keyColumns, Query que
541541

542542
if (!CollectionUtils.isEmpty(query.getColumns())) {
543543
for (SqlIdentifier columnName : query.getColumns()) {
544-
columns.add(Column.create(columnName, table));
544+
545+
String columnNameString = columnName.getReference();
546+
RelationalPersistentProperty property = entity.getPersistentProperty(columnNameString);
547+
if (property != null) {
548+
549+
AggregatePath aggregatePath = mappingContext.getAggregatePath(
550+
mappingContext.getPersistentPropertyPath(columnNameString, entity.getTypeInformation()));
551+
gatherColumn(aggregatePath, joins, columns);
552+
} else {
553+
columns.add(Column.create(columnName, table));
554+
}
545555
}
546556
} else {
547557
for (PersistentPropertyPath<RelationalPersistentProperty> path : mappingContext
548558
.findPersistentPropertyPaths(entity.getType(), p -> true)) {
549559

550-
AggregatePath extPath = mappingContext.getAggregatePath(path);
560+
AggregatePath aggregatePath = mappingContext.getAggregatePath(path);
551561

552-
// add a join if necessary
553-
Join join = getJoin(extPath);
554-
if (join != null) {
555-
joins.add(join);
556-
}
557-
558-
Column column = getColumn(extPath);
559-
if (column != null) {
560-
columns.add(column);
561-
}
562+
gatherColumn(aggregatePath, joins, columns);
562563
}
563564
}
564565

@@ -569,6 +570,20 @@ private Projection getProjection(Collection<SqlIdentifier> keyColumns, Query que
569570
return new Projection(columns, joins);
570571
}
571572

573+
private void gatherColumn(AggregatePath aggregatePath, Set<Join> joins, Set<Expression> columns) {
574+
575+
// add a join if necessary
576+
Join join = getJoin(aggregatePath);
577+
if (join != null) {
578+
joins.add(join);
579+
}
580+
581+
Column column = getColumn(aggregatePath);
582+
if (column != null) {
583+
columns.add(column);
584+
}
585+
}
586+
572587
/**
573588
* Projection including its source joins.
574589
*

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ void selectByQueryWithColumnLimit() {
367367
String sql = sqlGenerator.selectByQuery(query, new MapSqlParameterSource());
368368

369369
assertThat(sql).contains( //
370-
"SELECT dummy_entity.id1, dummy_entity.alpha, dummy_entity.beta, dummy_entity.gamma", //
370+
"SELECT dummy_entity.id1 AS id1, dummy_entity.alpha, dummy_entity.beta, dummy_entity.gamma", //
371371
"FROM dummy_entity" //
372372
);
373373
}

0 commit comments

Comments
 (0)