diff --git a/pom.xml b/pom.xml index ebd310325..ec709663b 100644 --- a/pom.xml +++ b/pom.xml @@ -52,7 +52,6 @@ 1.3.0 - 1.37 0.4.0.BUILD-SNAPSHOT @@ -165,22 +164,14 @@ jmh - - com.github.mp911de.microbenchmark-runner - microbenchmark-runner-junit5 - ${mbr.version} - test - org.openjdk.jmh jmh-core - ${jmh.version} test org.openjdk.jmh jmh-generator-annprocess - ${jmh.version} test diff --git a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/SqlGenerator.java b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/SqlGenerator.java index 064ab5487..783f12f15 100644 --- a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/SqlGenerator.java +++ b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/SqlGenerator.java @@ -23,6 +23,7 @@ import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; import org.springframework.data.jdbc.repository.support.SimpleJdbcRepository; import org.springframework.data.mapping.PersistentPropertyPath; +import org.springframework.data.mapping.context.InvalidPersistentPropertyPath; import org.springframework.data.mapping.context.MappingContext; import org.springframework.data.relational.core.dialect.Dialect; import org.springframework.data.relational.core.dialect.RenderContextFactory; @@ -36,10 +37,10 @@ import org.springframework.data.relational.core.sql.*; import org.springframework.data.relational.core.sql.render.RenderContext; import org.springframework.data.relational.core.sql.render.SqlRenderer; import org.springframework.data.util.Lazy; +import org.springframework.data.util.Predicates; import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; import org.springframework.lang.Nullable; import org.springframework.util.Assert; -import org.springframework.util.CollectionUtils; /** * Generates SQL statements to be used by {@link SimpleJdbcRepository} @@ -539,27 +540,26 @@ class SqlGenerator { Set columns = new LinkedHashSet<>(); Set joins = new LinkedHashSet<>(); - if (!CollectionUtils.isEmpty(query.getColumns())) { - for (SqlIdentifier columnName : query.getColumns()) { + for (SqlIdentifier columnName : query.getColumns()) { - String columnNameString = columnName.getReference(); - RelationalPersistentProperty property = entity.getPersistentProperty(columnNameString); - if (property != null) { + try { + AggregatePath aggregatePath = mappingContext.getAggregatePath( + mappingContext.getPersistentPropertyPath(columnName.getReference(), entity.getTypeInformation())); - AggregatePath aggregatePath = mappingContext.getAggregatePath( - mappingContext.getPersistentPropertyPath(columnNameString, entity.getTypeInformation())); - gatherColumn(aggregatePath, joins, columns); - } else { - columns.add(Column.create(columnName, table)); - } + includeColumnAndJoin(aggregatePath, joins, columns); + } catch (InvalidPersistentPropertyPath e) { + columns.add(Column.create(columnName, table)); } - } else { + } + + if (columns.isEmpty()) { + for (PersistentPropertyPath path : mappingContext - .findPersistentPropertyPaths(entity.getType(), p -> true)) { + .findPersistentPropertyPaths(entity.getType(), Predicates.isTrue())) { AggregatePath aggregatePath = mappingContext.getAggregatePath(path); - gatherColumn(aggregatePath, joins, columns); + includeColumnAndJoin(aggregatePath, joins, columns); } } @@ -570,7 +570,8 @@ class SqlGenerator { return new Projection(columns, joins); } - private void gatherColumn(AggregatePath aggregatePath, Set joins, Set columns) { + private void includeColumnAndJoin(AggregatePath aggregatePath, Collection joins, + Collection columns) { joins.addAll(getJoins(aggregatePath)); @@ -639,10 +640,7 @@ class SqlGenerator { // Simple entities without id include there backreference as a synthetic id in order to distinguish null entities // from entities with only null values. - if (path.isQualified() // - || path.isCollectionLike() // - || path.hasIdProperty() // - ) { + if (path.isQualified() || path.isCollectionLike() || path.hasIdProperty()) { return null; } diff --git a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/convert/SqlGeneratorUnitTests.java b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/convert/SqlGeneratorUnitTests.java index 51637b72a..62e95245d 100644 --- a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/convert/SqlGeneratorUnitTests.java +++ b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/convert/SqlGeneratorUnitTests.java @@ -32,6 +32,7 @@ import java.util.Set; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; + import org.springframework.data.annotation.Id; import org.springframework.data.annotation.ReadOnlyProperty; import org.springframework.data.annotation.Version; @@ -399,7 +400,7 @@ class SqlGeneratorUnitTests { assertThat(sql).contains( // "SELECT", // - "ref.id1 AS id1, ref.content AS x_content", // + "ref.x_content AS ref_x_content", // "FROM dummy_entity", // "LEFT OUTER JOIN referenced_entity ref ON ref.dummy_entity = dummy_entity.id1"); }