Browse Source

Polishing.

Resolve mapped property paths to aggregate paths. Add tests.
Clean up unused profile declarations.

See #1803
Original pull request: #1967
pull/2069/merge
Mark Paluch 6 months ago
parent
commit
a1f104b004
No known key found for this signature in database
GPG Key ID: 55BC6374BAA9D973
  1. 9
      pom.xml
  2. 38
      spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/SqlGenerator.java
  3. 3
      spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/convert/SqlGeneratorUnitTests.java

9
pom.xml

@ -52,7 +52,6 @@
<!-- test dependencies --> <!-- test dependencies -->
<archunit.version>1.3.0</archunit.version> <archunit.version>1.3.0</archunit.version>
<jmh.version>1.37</jmh.version>
<mbr.version>0.4.0.BUILD-SNAPSHOT</mbr.version> <mbr.version>0.4.0.BUILD-SNAPSHOT</mbr.version>
</properties> </properties>
@ -165,22 +164,14 @@
<profile> <profile>
<id>jmh</id> <id>jmh</id>
<dependencies> <dependencies>
<dependency>
<groupId>com.github.mp911de.microbenchmark-runner</groupId>
<artifactId>microbenchmark-runner-junit5</artifactId>
<version>${mbr.version}</version>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>org.openjdk.jmh</groupId> <groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId> <artifactId>jmh-core</artifactId>
<version>${jmh.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.openjdk.jmh</groupId> <groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId> <artifactId>jmh-generator-annprocess</artifactId>
<version>${jmh.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>

38
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.domain.Sort;
import org.springframework.data.jdbc.repository.support.SimpleJdbcRepository; import org.springframework.data.jdbc.repository.support.SimpleJdbcRepository;
import org.springframework.data.mapping.PersistentPropertyPath; import org.springframework.data.mapping.PersistentPropertyPath;
import org.springframework.data.mapping.context.InvalidPersistentPropertyPath;
import org.springframework.data.mapping.context.MappingContext; import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.relational.core.dialect.Dialect; import org.springframework.data.relational.core.dialect.Dialect;
import org.springframework.data.relational.core.dialect.RenderContextFactory; 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.RenderContext;
import org.springframework.data.relational.core.sql.render.SqlRenderer; import org.springframework.data.relational.core.sql.render.SqlRenderer;
import org.springframework.data.util.Lazy; import org.springframework.data.util.Lazy;
import org.springframework.data.util.Predicates;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
/** /**
* Generates SQL statements to be used by {@link SimpleJdbcRepository} * Generates SQL statements to be used by {@link SimpleJdbcRepository}
@ -539,27 +540,26 @@ class SqlGenerator {
Set<Expression> columns = new LinkedHashSet<>(); Set<Expression> columns = new LinkedHashSet<>();
Set<Join> joins = new LinkedHashSet<>(); Set<Join> joins = new LinkedHashSet<>();
if (!CollectionUtils.isEmpty(query.getColumns())) { for (SqlIdentifier columnName : query.getColumns()) {
for (SqlIdentifier columnName : query.getColumns()) {
String columnNameString = columnName.getReference(); try {
RelationalPersistentProperty property = entity.getPersistentProperty(columnNameString); AggregatePath aggregatePath = mappingContext.getAggregatePath(
if (property != null) { mappingContext.getPersistentPropertyPath(columnName.getReference(), entity.getTypeInformation()));
AggregatePath aggregatePath = mappingContext.getAggregatePath( includeColumnAndJoin(aggregatePath, joins, columns);
mappingContext.getPersistentPropertyPath(columnNameString, entity.getTypeInformation())); } catch (InvalidPersistentPropertyPath e) {
gatherColumn(aggregatePath, joins, columns); columns.add(Column.create(columnName, table));
} else {
columns.add(Column.create(columnName, table));
}
} }
} else { }
if (columns.isEmpty()) {
for (PersistentPropertyPath<RelationalPersistentProperty> path : mappingContext for (PersistentPropertyPath<RelationalPersistentProperty> path : mappingContext
.findPersistentPropertyPaths(entity.getType(), p -> true)) { .findPersistentPropertyPaths(entity.getType(), Predicates.isTrue())) {
AggregatePath aggregatePath = mappingContext.getAggregatePath(path); AggregatePath aggregatePath = mappingContext.getAggregatePath(path);
gatherColumn(aggregatePath, joins, columns); includeColumnAndJoin(aggregatePath, joins, columns);
} }
} }
@ -570,7 +570,8 @@ class SqlGenerator {
return new Projection(columns, joins); return new Projection(columns, joins);
} }
private void gatherColumn(AggregatePath aggregatePath, Set<Join> joins, Set<Expression> columns) { private void includeColumnAndJoin(AggregatePath aggregatePath, Collection<Join> joins,
Collection<Expression> columns) {
joins.addAll(getJoins(aggregatePath)); 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 // Simple entities without id include there backreference as a synthetic id in order to distinguish null entities
// from entities with only null values. // from entities with only null values.
if (path.isQualified() // if (path.isQualified() || path.isCollectionLike() || path.hasIdProperty()) {
|| path.isCollectionLike() //
|| path.hasIdProperty() //
) {
return null; return null;
} }

3
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.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.data.annotation.Id; import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.ReadOnlyProperty; import org.springframework.data.annotation.ReadOnlyProperty;
import org.springframework.data.annotation.Version; import org.springframework.data.annotation.Version;
@ -399,7 +400,7 @@ class SqlGeneratorUnitTests {
assertThat(sql).contains( // assertThat(sql).contains( //
"SELECT", // "SELECT", //
"ref.id1 AS id1, ref.content AS x_content", // "ref.x_content AS ref_x_content", //
"FROM dummy_entity", // "FROM dummy_entity", //
"LEFT OUTER JOIN referenced_entity ref ON ref.dummy_entity = dummy_entity.id1"); "LEFT OUTER JOIN referenced_entity ref ON ref.dummy_entity = dummy_entity.id1");
} }

Loading…
Cancel
Save