@@ -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 *
0 commit comments