2323import org .springframework .data .domain .Sort ;
2424import org .springframework .data .jdbc .repository .support .SimpleJdbcRepository ;
2525import org .springframework .data .mapping .PersistentPropertyPath ;
26+ import org .springframework .data .mapping .context .InvalidPersistentPropertyPath ;
2627import org .springframework .data .mapping .context .MappingContext ;
2728import org .springframework .data .relational .core .dialect .Dialect ;
2829import org .springframework .data .relational .core .dialect .RenderContextFactory ;
3637import org .springframework .data .relational .core .sql .render .RenderContext ;
3738import org .springframework .data .relational .core .sql .render .SqlRenderer ;
3839import org .springframework .data .util .Lazy ;
40+ import org .springframework .data .util .Predicates ;
3941import org .springframework .jdbc .core .namedparam .MapSqlParameterSource ;
4042import org .springframework .lang .Nullable ;
4143import org .springframework .util .Assert ;
42- import org .springframework .util .CollectionUtils ;
4344
4445/**
4546 * Generates SQL statements to be used by {@link SimpleJdbcRepository}
@@ -539,27 +540,26 @@ private Projection getProjection(Collection<SqlIdentifier> keyColumns, Query que
539540 Set <Expression > columns = new LinkedHashSet <>();
540541 Set <Join > joins = new LinkedHashSet <>();
541542
542- if (!CollectionUtils .isEmpty (query .getColumns ())) {
543- for (SqlIdentifier columnName : query .getColumns ()) {
543+ for (SqlIdentifier columnName : query .getColumns ()) {
544544
545- String columnNameString = columnName . getReference ();
546- RelationalPersistentProperty property = entity . getPersistentProperty ( columnNameString );
547- if ( property != null ) {
545+ try {
546+ AggregatePath aggregatePath = mappingContext . getAggregatePath (
547+ mappingContext . getPersistentPropertyPath ( columnName . getReference (), entity . getTypeInformation ()));
548548
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- }
549+ includeColumnAndJoin (aggregatePath , joins , columns );
550+ } catch (InvalidPersistentPropertyPath e ) {
551+ columns .add (Column .create (columnName , table ));
555552 }
556- } else {
553+ }
554+
555+ if (columns .isEmpty ()) {
556+
557557 for (PersistentPropertyPath <RelationalPersistentProperty > path : mappingContext
558- .findPersistentPropertyPaths (entity .getType (), p -> true )) {
558+ .findPersistentPropertyPaths (entity .getType (), Predicates . isTrue () )) {
559559
560560 AggregatePath aggregatePath = mappingContext .getAggregatePath (path );
561561
562- gatherColumn (aggregatePath , joins , columns );
562+ includeColumnAndJoin (aggregatePath , joins , columns );
563563 }
564564 }
565565
@@ -570,7 +570,7 @@ private Projection getProjection(Collection<SqlIdentifier> keyColumns, Query que
570570 return new Projection (columns , joins );
571571 }
572572
573- private void gatherColumn (AggregatePath aggregatePath , Set <Join > joins , Set <Expression > columns ) {
573+ private void includeColumnAndJoin (AggregatePath aggregatePath , Set <Join > joins , Set <Expression > columns ) {
574574
575575 // add a join if necessary
576576 Join join = getJoin (aggregatePath );
@@ -643,10 +643,7 @@ Column getColumn(AggregatePath path) {
643643 // Simple entities without id include there backreference as a synthetic id in order to distinguish null entities
644644 // from entities with only null values.
645645
646- if (path .isQualified () //
647- || path .isCollectionLike () //
648- || path .hasIdProperty () //
649- ) {
646+ if (path .isQualified () || path .isCollectionLike () || path .hasIdProperty ()) {
650647 return null ;
651648 }
652649
0 commit comments