Skip to content

Commit 74d3708

Browse files
committed
backReferenceColumnInfo
1 parent b60f28b commit 74d3708

File tree

10 files changed

+63
-68
lines changed

10 files changed

+63
-68
lines changed

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/JdbcIdentifierBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public static Identifier forBackReference(JdbcConverter converter, AggregatePath
6565
// references and possibly keys, that form an id
6666
if (idDefiningParentPath.hasIdProperty()) {
6767

68-
AggregatePath.ColumnInfos infos = path.getTableInfo().reverseColumnInfos();
68+
AggregatePath.ColumnInfos infos = path.getTableInfo().backReferenceColumnInfos();
6969
identifierToUse = infos.reduce(Identifier.empty(), (ap, ci) -> {
7070

7171
RelationalPersistentProperty property = ap.getRequiredLeafProperty();

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/MappingJdbcConverter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ public boolean hasValue(RelationalPersistentProperty property) {
408408
return delegate.hasValue(toUse);
409409
}
410410

411-
return delegate.hasValue(aggregatePath.getTableInfo().reverseColumnInfos().any().alias());
411+
return delegate.hasValue(aggregatePath.getTableInfo().backReferenceColumnInfos().any().alias());
412412
}
413413

414414
return delegate.hasValue(aggregatePath);
@@ -434,7 +434,7 @@ public boolean hasNonEmptyValue(RelationalPersistentProperty property) {
434434
return delegate.hasValue(toUse);
435435
}
436436

437-
return delegate.hasValue(aggregatePath.getTableInfo().reverseColumnInfos().any().alias());
437+
return delegate.hasValue(aggregatePath.getTableInfo().backReferenceColumnInfos().any().alias());
438438
}
439439

440440
return delegate.hasNonEmptyValue(aggregatePath);

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/SqlContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ Column getColumn(AggregatePath path) {
6969
*/
7070
Column getAnyReverseColumn(AggregatePath path) {
7171

72-
AggregatePath.ColumnInfo columnInfo = path.getTableInfo().reverseColumnInfos().any();
72+
AggregatePath.ColumnInfo columnInfo = path.getTableInfo().backReferenceColumnInfos().any();
7373
return getTable(path).column(columnInfo.name()).as(columnInfo.alias());
7474
}
7575
}

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/SqlGenerator.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import org.springframework.data.relational.core.sql.*;
3636
import org.springframework.data.relational.core.sql.render.SqlRenderer;
3737
import org.springframework.data.util.Lazy;
38-
import org.springframework.data.util.Pair;
3938
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
4039
import org.springframework.lang.Nullable;
4140
import org.springframework.util.Assert;
@@ -684,7 +683,7 @@ Join getJoin(AggregatePath path) {
684683
}
685684

686685
Table currentTable = sqlContext.getTable(path);
687-
AggregatePath.ColumnInfos backRefColumnInfos = path.getTableInfo().reverseColumnInfos();
686+
AggregatePath.ColumnInfos backRefColumnInfos = path.getTableInfo().backReferenceColumnInfos();
688687

689688
AggregatePath idDefiningParentPath = path.getIdDefiningParentPath();
690689
Table parentTable = sqlContext.getTable(idDefiningParentPath);
@@ -840,7 +839,7 @@ private String createDeleteByPathAndCriteria(AggregatePath path,
840839
Delete delete;
841840

842841
Map<AggregatePath, Column> columns = new TreeMap<>();
843-
AggregatePath.ColumnInfos columnInfos = path.getTableInfo().reverseColumnInfos();
842+
AggregatePath.ColumnInfos columnInfos = path.getTableInfo().backReferenceColumnInfos();
844843
columnInfos.forEach((ag, ci) -> columns.put(ag, table.column(ci.name())));
845844

846845
if (isFirstNonRoot(path)) {

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/JdbcDeleteQueryCreator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ private void deleteRelations(List<Delete> deleteChain, RelationalPersistentEntit
138138
// MariaDB prior to 11.6 does not support aliases for delete statements
139139
Table table = sqlContext.getUnaliasedTable(aggregatePath);
140140

141-
List<Column> reverseColumns = aggregatePath.getTableInfo().reverseColumnInfos()
141+
List<Column> reverseColumns = aggregatePath.getTableInfo().backReferenceColumnInfos()
142142
.toList(ci -> table.column(ci.name()));
143143
Expression expression = TupleExpression.maybeWrap(reverseColumns);
144144

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/JdbcQueryCreator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ Join getJoin(SqlContext sqlContext, AggregatePath path) {
325325
AggregatePath idDefiningParentPath = path.getIdDefiningParentPath();
326326
Table parentTable = sqlContext.getTable(idDefiningParentPath);
327327

328-
List<Column> reverseColumns = path.getTableInfo().reverseColumnInfos().toList(ci -> currentTable.column(ci.name()));
328+
List<Column> reverseColumns = path.getTableInfo().backReferenceColumnInfos().toList(ci -> currentTable.column(ci.name()));
329329
List<Column> idColumns = idDefiningParentPath.getTableInfo().idColumnInfos()
330330
.toList(ci -> parentTable.column(ci.name()));
331331
return new Join( //

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/SqlContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Column getColumn(AggregatePath path) {
4747

4848
Column getAnyReverseColumn(AggregatePath path) {
4949

50-
AggregatePath.ColumnInfo anyReverseColumnInfo = path.getTableInfo().reverseColumnInfos().any();
50+
AggregatePath.ColumnInfo anyReverseColumnInfo = path.getTableInfo().backReferenceColumnInfos().any();
5151
return getTable(path).column(anyReverseColumnInfo.name()).as(anyReverseColumnInfo.alias());
5252
}
5353

spring-data-relational/src/main/java/org/springframework/data/relational/core/mapping/AggregatePath.java

Lines changed: 50 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@
4040
* Represents a path within an aggregate starting from the aggregate root. The path can be iterated from the leaf to its
4141
* root.
4242
*
43-
* @since 3.2
4443
* @author Jens Schauder
4544
* @author Mark Paluch
45+
* @since 3.2
4646
*/
4747
public interface AggregatePath extends Iterable<AggregatePath>, Comparable<AggregatePath> {
4848

@@ -242,42 +242,36 @@ default Stream<AggregatePath> stream() {
242242
* Returns the longest ancestor path that has an {@link org.springframework.data.annotation.Id} property.
243243
*
244244
* @return A path that starts just as this path but is shorter. Guaranteed to be not {@literal null}. TODO: throws
245-
* NoSuchElementException: No value present for empty paths
245+
* NoSuchElementException: No value present for empty paths
246246
*/
247247
AggregatePath getIdDefiningParentPath();
248248

249+
/**
250+
* The path resulting from removing the first element of the {@link AggregatePath}.
251+
*
252+
* @return {@literal null} for any {@link AggregatePath} having less than two elements.
253+
* @since 3.5.0
254+
*/
249255
@Nullable
250256
AggregatePath getTail();
251257

258+
/**
259+
* Information about a table underlying an entity.
260+
*
261+
* @param qualifiedTableName the fully qualified name of the table this path is tied to or of the longest ancestor path that is actually
262+
* tied to a table. Must not be {@literal null}.
263+
* @param tableAlias the alias used for the table on which this path is based. May be {@literal null}.
264+
* @param backReferenceColumnInfos information about the columns used to reference back to the owning entity. Must not be {@literal null}. Since 3.5.
265+
* @param qualifierColumnInfo the column used for the list index or map key of the leaf property of this path. May be {@literal null}.
266+
* @param qualifierColumnType the type of the qualifier column of the leaf property of this path or {@literal null} if this is not applicable. May be {@literal null}.
267+
* @param idColumnInfos the column name of the id column of the ancestor path that represents an actual table. Must not be {@literal null}.
268+
*/
252269
record TableInfo(
253-
254-
/*
255-
* The fully qualified name of the table this path is tied to or of the longest ancestor path that is actually
256-
* tied to a table.
257-
*/
258270
SqlIdentifier qualifiedTableName,
259-
260-
/*
261-
* The alias used for the table on which this path is based.
262-
*/
263271
@Nullable SqlIdentifier tableAlias,
264-
265-
ColumnInfos reverseColumnInfos,
266-
267-
/*
268-
* The column used for the list index or map key of the leaf property of this path.
269-
*/
272+
ColumnInfos backReferenceColumnInfos,
270273
@Nullable ColumnInfo qualifierColumnInfo,
271-
272-
/*
273-
* The type of the qualifier column of the leaf property of this path or {@literal null} if this is not
274-
* applicable.
275-
*/
276274
@Nullable Class<?> qualifierColumnType,
277-
278-
/*
279-
* The column name of the id column of the ancestor path that represents an actual table.
280-
*/
281275
ColumnInfos idColumnInfos) {
282276

283277
static TableInfo of(AggregatePath path) {
@@ -313,7 +307,7 @@ static TableInfo of(AggregatePath path) {
313307
}
314308

315309
private static ColumnInfos computeIdColumnInfos(AggregatePath tableOwner,
316-
RelationalPersistentEntity<?> leafEntity) {
310+
RelationalPersistentEntity<?> leafEntity) {
317311

318312
ColumnInfos idColumnInfos = ColumnInfos.empty(tableOwner);
319313
if (!leafEntity.hasIdProperty()) {
@@ -394,16 +388,16 @@ private static ColumnInfos computeReverseColumnInfo(AggregatePath path) {
394388

395389
@Deprecated(forRemoval = true)
396390
public ColumnInfo reverseColumnInfo() {
397-
return reverseColumnInfos.unique();
391+
return backReferenceColumnInfos.unique();
398392
}
399393

400394
public ColumnInfos effectiveIdColumnInfos() {
401-
return reverseColumnInfos.columnInfos.isEmpty() ? idColumnInfos : reverseColumnInfos;
395+
return backReferenceColumnInfos.columnInfos.isEmpty() ? idColumnInfos : backReferenceColumnInfos;
402396
}
403397
}
404398

405399
/**
406-
* @param name The name of the column used to represent this property in the database.
400+
* @param name The name of the column used to represent this property in the database.
407401
* @param alias The alias for the column used to represent this property in the database.
408402
*/
409403
record ColumnInfo(SqlIdentifier name, SqlIdentifier alias) {
@@ -415,7 +409,7 @@ record ColumnInfo(SqlIdentifier name, SqlIdentifier alias) {
415409
* @param path the path to the {@literal ColumnInfo} for.
416410
* @return the {@link ColumnInfo}.
417411
* @throws IllegalArgumentException if the path is {@link #isRoot()}, {@link #isEmbedded()} or
418-
* {@link #isMultiValued()}.
412+
* {@link #isMultiValued()}.
419413
*/
420414
static ColumnInfo of(AggregatePath path) {
421415

@@ -435,7 +429,7 @@ static ColumnInfo of(AggregatePath path) {
435429
* A group of {@link ColumnInfo} values referenced by there respective {@link AggregatePath}. It is used in a similar
436430
* way as {@literal ColumnInfo} when one needs to consider more than a single column. This is relevant for composite
437431
* ids and references to such ids.
438-
*
432+
*
439433
* @author Jens Schauder
440434
**/
441435
class ColumnInfos {
@@ -445,9 +439,9 @@ class ColumnInfos {
445439

446440
/**
447441
* Creates a new ColumnInfos instances based on the arguments.
448-
*
449-
* @param basePath The path on which all other paths in the other argument are based on. For the typical case of a
450-
* composite id, this would be the path to the composite ids.
442+
*
443+
* @param basePath The path on which all other paths in the other argument are based on. For the typical case of a
444+
* composite id, this would be the path to the composite ids.
451445
* @param columnInfos A map, mapping {@literal AggregatePath} instances to the respective {@literal ColumnInfo}
452446
*/
453447
private ColumnInfos(AggregatePath basePath, Map<AggregatePath, ColumnInfo> columnInfos) {
@@ -482,7 +476,7 @@ public ColumnInfo unique() {
482476

483477
/**
484478
* Any of the contained {@link ColumnInfo} instances.
485-
*
479+
*
486480
* @return a {@link ColumnInfo} instance.
487481
* @throws java.util.NoSuchElementException if no instance is available.
488482
*/
@@ -494,7 +488,7 @@ public ColumnInfo any() {
494488

495489
/**
496490
* Checks if {@literal this} instance is empty, i.e. does not contain any {@link ColumnInfo} instance.
497-
*
491+
*
498492
* @return {@literal true} iff the collection of {@literal ColumnInfo} is empty.
499493
*/
500494
public boolean isEmpty() {
@@ -503,10 +497,10 @@ public boolean isEmpty() {
503497

504498
/**
505499
* Applies a function to all the {@link ColumnInfo} instances and returns the result in a list.
506-
*
500+
*
507501
* @param mapper the function to be applied
502+
* @param <T> the type returned by {@literal mapper} and contained in the resulting {@literal List}
508503
* @return the list of results from mapper.
509-
* @param <T> the type returned by {@literal mapper} and contained in the resulting {@literal List}
510504
*/
511505
public <T> List<T> toList(Function<ColumnInfo, T> mapper) {
512506
return columnInfos.values().stream().map(mapper).toList();
@@ -520,13 +514,13 @@ public <T> List<T> toList(Function<ColumnInfo, T> mapper) {
520514
* {@link BinaryOperator combiner} is called with the current state (or initial {@code identity}) and the
521515
* accumulated {@code T} state to combine both into a single return value.
522516
*
523-
* @param identity the identity (initial) value for the combiner function.
517+
* @param identity the identity (initial) value for the combiner function.
524518
* @param accumulator an associative, non-interfering (free of side effects), stateless function for incorporating
525-
* an additional element into a result.
526-
* @param combiner an associative, non-interfering, stateless function for combining two values, which must be
527-
* compatible with the {@code accumulator} function.
519+
* an additional element into a result.
520+
* @param combiner an associative, non-interfering, stateless function for combining two values, which must be
521+
* compatible with the {@code accumulator} function.
522+
* @param <T> type of the result.
528523
* @return result of the function.
529-
* @param <T> type of the result.
530524
* @since 3.5
531525
*/
532526
public <T> T reduce(T identity, BiFunction<AggregatePath, ColumnInfo, T> accumulator, BinaryOperator<T> combiner) {
@@ -544,7 +538,7 @@ public <T> T reduce(T identity, BiFunction<AggregatePath, ColumnInfo, T> accumul
544538

545539
/**
546540
* Calls the consumer for each pair of {@link AggregatePath} and {@literal ColumnInfo}.
547-
*
541+
*
548542
* @param consumer the function to call.
549543
*/
550544
public void forEach(BiConsumer<AggregatePath, ColumnInfo> consumer) {
@@ -567,7 +561,7 @@ public <T> T any(BiFunction<AggregatePath, ColumnInfo, T> mapper) {
567561

568562
/**
569563
* Gets the {@link ColumnInfo} for the provided {@link AggregatePath}
570-
*
564+
*
571565
* @param path for which to return the {@literal ColumnInfo}
572566
* @return {@literal ColumnInfo} for the given path.
573567
*/
@@ -577,7 +571,7 @@ public ColumnInfo get(AggregatePath path) {
577571

578572
/**
579573
* Constructs an {@link AggregatePath} from the {@literal basePath} and the provided argument.
580-
*
574+
*
581575
* @param ap {@literal AggregatePath} to be appended to the {@literal basePath}.
582576
* @return the combined (@literal AggregatePath}
583577
*/
@@ -597,7 +591,7 @@ public int size() {
597591

598592
/**
599593
* A builder for {@link ColumnInfos} instances.
600-
*
594+
*
601595
* @author Jens Schauder
602596
*/
603597
class ColumInfosBuilder {
@@ -617,8 +611,8 @@ public ColumInfosBuilder(AggregatePath basePath) {
617611
/**
618612
* Adds a {@link ColumnInfo} to the {@link ColumnInfos} under construction.
619613
*
620-
* @param path referencing the {@literal ColumnInfo}.
621-
* @param name of the column.
614+
* @param path referencing the {@literal ColumnInfo}.
615+
* @param name of the column.
622616
* @param alias alias for the column.
623617
*/
624618
void add(AggregatePath path, SqlIdentifier name, SqlIdentifier alias) {
@@ -629,8 +623,8 @@ void add(AggregatePath path, SqlIdentifier name, SqlIdentifier alias) {
629623
* Adds a {@link ColumnInfo} to the {@link ColumnInfos} under construction.
630624
*
631625
* @param property referencing the {@literal ColumnInfo}.
632-
* @param name of the column.
633-
* @param alias alias for the column.
626+
* @param name of the column.
627+
* @param alias alias for the column.
634628
*/
635629
public void add(RelationalPersistentProperty property, SqlIdentifier name, SqlIdentifier alias) {
636630
add(basePath.append(property), name, alias);
@@ -639,15 +633,16 @@ public void add(RelationalPersistentProperty property, SqlIdentifier name, SqlId
639633
/**
640634
* Adds a {@link ColumnInfo} to the {@link ColumnInfos} under construction.
641635
*
642-
* @param path the path referencing the {@literal ColumnInfo}
636+
* @param path the path referencing the {@literal ColumnInfo}
643637
* @param columnInfo the {@literal ColumnInfo} added.
644-
*
645638
*/
646639
public void add(AggregatePath path, ColumnInfo columnInfo) {
647640
columnInfoMap.put(path.subtract(basePath), columnInfo);
648641
}
642+
649643
/**
650644
* Build the final {@link ColumnInfos} instance.
645+
*
651646
* @return a {@literal ColumnInfos} instance containing all the added {@link ColumnInfo} instances.
652647
*/
653648
ColumnInfos build() {
@@ -658,6 +653,7 @@ ColumnInfos build() {
658653

659654
/**
660655
* Subtract the {@literal basePath} from {@literal this} {@literal AggregatePath} by removing the {@literal basePath} from the beginning of {@literal this}.
656+
*
661657
* @param basePath the path to be removed.
662658
* @return an AggregatePath that ends like the original {@literal AggregatePath} but has {@literal basePath} removed from the beginning.
663659
*/

spring-data-relational/src/main/java/org/springframework/data/relational/core/sqlgeneration/SingleQuerySqlGenerator.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ private QueryMeta createInlineQuery(AggregatePath basePath, @Nullable Condition
170170
Expression count = basePath.isRoot() ? new AliasedExpression(SQL.literalOf(1), rowCountAlias) //
171171
: AnalyticFunction.create("count", Expressions.just("*")) //
172172
.partitionBy( //
173-
basePath.getTableInfo().reverseColumnInfos().toList( //
173+
basePath.getTableInfo().backReferenceColumnInfos().toList( //
174174
ci -> table.column(ci.name()) //
175175
) //
176176
).as(rowCountAlias);
@@ -182,7 +182,7 @@ private QueryMeta createInlineQuery(AggregatePath basePath, @Nullable Condition
182182
if (!basePath.isRoot()) {
183183

184184
backReferenceAlias = aliases.getBackReferenceAlias(basePath);
185-
columns.add(table.column(basePath.getTableInfo().reverseColumnInfos().unique().name()).as(backReferenceAlias));
185+
columns.add(table.column(basePath.getTableInfo().backReferenceColumnInfos().unique().name()).as(backReferenceAlias));
186186

187187
keyAlias = aliases.getKeyAlias(basePath);
188188
Expression keyExpression = basePath.isQualified()
@@ -242,7 +242,7 @@ private String getIdentifierProperty(List<AggregatePath> paths) {
242242

243243
private static AnalyticFunction createRowNumberExpression(AggregatePath basePath, Table table,
244244
String rowNumberAlias) {
245-
AggregatePath.ColumnInfos reverseColumnInfos = basePath.getTableInfo().reverseColumnInfos();
245+
AggregatePath.ColumnInfos reverseColumnInfos = basePath.getTableInfo().backReferenceColumnInfos();
246246
return AnalyticFunction.create("row_number") //
247247
.partitionBy(reverseColumnInfos.toList(ci -> table.column(ci.name()))) //
248248
.orderBy(reverseColumnInfos.toList(ci -> table.column(ci.name()))) //

spring-data-relational/src/test/java/org/springframework/data/relational/core/mapping/DefaultAggregatePathUnitTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ void reverseColumnName() {
147147
void reverseColumnNames() {
148148

149149
assertSoftly(softly -> {
150-
softly.assertThat(path(CompoundIdEntity.class, "second").getTableInfo().reverseColumnInfos().toList(x -> x))
150+
softly.assertThat(path(CompoundIdEntity.class, "second").getTableInfo().backReferenceColumnInfos().toList(x -> x))
151151
.extracting(AggregatePath.ColumnInfo::name)
152152
.containsExactlyInAnyOrder(quoted("COMPOUND_ID_ENTITY_ONE"), quoted("COMPOUND_ID_ENTITY_TWO"));
153153

0 commit comments

Comments
 (0)