Skip to content

Commit

Permalink
Consider query columns as property names.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
schauder committed Jan 20, 2025
1 parent 9c2a954 commit 2326e23
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -541,24 +541,25 @@ private Projection getProjection(Collection<SqlIdentifier> keyColumns, Query que

if (!CollectionUtils.isEmpty(query.getColumns())) {
for (SqlIdentifier columnName : query.getColumns()) {
columns.add(Column.create(columnName, table));

String columnNameString = columnName.getReference();
RelationalPersistentProperty property = entity.getPersistentProperty(columnNameString);
if (property != null) {

AggregatePath aggregatePath = mappingContext.getAggregatePath(
mappingContext.getPersistentPropertyPath(columnNameString, entity.getTypeInformation()));
gatherColumn(aggregatePath, joins, columns);
} else {
columns.add(Column.create(columnName, table));
}
}
} else {
for (PersistentPropertyPath<RelationalPersistentProperty> path : mappingContext
.findPersistentPropertyPaths(entity.getType(), p -> true)) {

AggregatePath extPath = mappingContext.getAggregatePath(path);
AggregatePath aggregatePath = mappingContext.getAggregatePath(path);

// add a join if necessary
Join join = getJoin(extPath);
if (join != null) {
joins.add(join);
}

Column column = getColumn(extPath);
if (column != null) {
columns.add(column);
}
gatherColumn(aggregatePath, joins, columns);
}
}

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

private void gatherColumn(AggregatePath aggregatePath, Set<Join> joins, Set<Expression> columns) {

// add a join if necessary
Join join = getJoin(aggregatePath);
if (join != null) {
joins.add(join);
}

Column column = getColumn(aggregatePath);
if (column != null) {
columns.add(column);
}
}

/**
* Projection including its source joins.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ void selectByQueryWithColumnLimit() {
String sql = sqlGenerator.selectByQuery(query, new MapSqlParameterSource());

assertThat(sql).contains( //
"SELECT dummy_entity.id1, dummy_entity.alpha, dummy_entity.beta, dummy_entity.gamma", //
"SELECT dummy_entity.id1 AS id1, dummy_entity.alpha, dummy_entity.beta, dummy_entity.gamma", //
"FROM dummy_entity" //
);
}
Expand Down

0 comments on commit 2326e23

Please sign in to comment.