Skip to content

Commit ffab841

Browse files
committed
Polishing
1 parent eadd4f1 commit ffab841

30 files changed

+249
-170
lines changed

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

+44-25
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,19 @@ class JdbcAggregateChangeExecutionContext {
7373

7474
<T> void executeInsertRoot(DbAction.InsertRoot<T> insert) {
7575

76-
Object id = accessStrategy.insert(insert.getEntity(), insert.getEntityType(), Identifier.empty(), insert.getIdValueSource());
76+
Object id = accessStrategy.insert(insert.getEntity(), insert.getEntityType(), Identifier.empty(),
77+
insert.getIdValueSource());
7778
add(new DbActionExecutionResult(insert, id));
7879
}
7980

8081
<T> void executeBatchInsertRoot(DbAction.BatchInsertRoot<T> batchInsertRoot) {
8182

8283
List<DbAction.InsertRoot<T>> inserts = batchInsertRoot.getActions();
83-
List<InsertSubject<T>> insertSubjects = inserts.stream().map(insert -> InsertSubject.describedBy(insert.getEntity(), Identifier.empty())).collect(Collectors.toList());
84+
List<InsertSubject<T>> insertSubjects = inserts.stream()
85+
.map(insert -> InsertSubject.describedBy(insert.getEntity(), Identifier.empty())).collect(Collectors.toList());
8486

85-
Object[] ids = accessStrategy.insert(insertSubjects, batchInsertRoot.getEntityType(), batchInsertRoot.getBatchValue());
87+
Object[] ids = accessStrategy.insert(insertSubjects, batchInsertRoot.getEntityType(),
88+
batchInsertRoot.getBatchValue());
8689

8790
for (int i = 0; i < inserts.size(); i++) {
8891
add(new DbActionExecutionResult(inserts.get(i), ids.length > 0 ? ids[i] : null));
@@ -92,14 +95,17 @@ <T> void executeBatchInsertRoot(DbAction.BatchInsertRoot<T> batchInsertRoot) {
9295
<T> void executeInsert(DbAction.Insert<T> insert) {
9396

9497
Identifier parentKeys = getParentKeys(insert, converter);
95-
Object id = accessStrategy.insert(insert.getEntity(), insert.getEntityType(), parentKeys, insert.getIdValueSource());
98+
Object id = accessStrategy.insert(insert.getEntity(), insert.getEntityType(), parentKeys,
99+
insert.getIdValueSource());
96100
add(new DbActionExecutionResult(insert, id));
97101
}
98102

99103
<T> void executeBatchInsert(DbAction.BatchInsert<T> batchInsert) {
100104

101105
List<DbAction.Insert<T>> inserts = batchInsert.getActions();
102-
List<InsertSubject<T>> insertSubjects = inserts.stream().map(insert -> InsertSubject.describedBy(insert.getEntity(), getParentKeys(insert, converter))).collect(Collectors.toList());
106+
List<InsertSubject<T>> insertSubjects = inserts.stream()
107+
.map(insert -> InsertSubject.describedBy(insert.getEntity(), getParentKeys(insert, converter)))
108+
.collect(Collectors.toList());
103109

104110
Object[] ids = accessStrategy.insert(insertSubjects, batchInsert.getEntityType(), batchInsert.getBatchValue());
105111

@@ -174,7 +180,8 @@ private Identifier getParentKeys(DbAction.WithDependingOn<?> action, JdbcConvert
174180
.forBackReferences(converter, context.getAggregatePath(action.getPropertyPath()),
175181
getValueProvider(id, context.getAggregatePath(action.getPropertyPath()), converter));
176182

177-
for (Map.Entry<PersistentPropertyPath<RelationalPersistentProperty>, Object> qualifier : action.getQualifiers().entrySet()) {
183+
for (Map.Entry<PersistentPropertyPath<RelationalPersistentProperty>, Object> qualifier : action.getQualifiers()
184+
.entrySet()) {
178185
identifier = identifier.withQualifier(context.getAggregatePath(qualifier.getKey()), qualifier.getValue());
179186
}
180187

@@ -183,7 +190,8 @@ private Identifier getParentKeys(DbAction.WithDependingOn<?> action, JdbcConvert
183190

184191
static Function<AggregatePath, Object> getValueProvider(Object idValue, AggregatePath path, JdbcConverter converter) {
185192

186-
RelationalPersistentEntity<?> entity = converter.getMappingContext().getPersistentEntity(path.getIdDefiningParentPath().getRequiredIdProperty().getType());
193+
RelationalPersistentEntity<?> entity = converter.getMappingContext()
194+
.getPersistentEntity(path.getIdDefiningParentPath().getRequiredIdProperty().getType());
187195

188196
Function<AggregatePath, Object> valueProvider = ap -> {
189197
if (entity == null) {
@@ -198,7 +206,8 @@ static Function<AggregatePath, Object> getValueProvider(Object idValue, Aggregat
198206

199207
private Object getParentId(DbAction.WithDependingOn<?> action) {
200208

201-
DbAction.WithEntity<?> idOwningAction = getIdOwningAction(action, context.getAggregatePath(action.getPropertyPath()).getIdDefiningParentPath());
209+
DbAction.WithEntity<?> idOwningAction = getIdOwningAction(action,
210+
context.getAggregatePath(action.getPropertyPath()).getIdDefiningParentPath());
202211

203212
return getPotentialGeneratedIdFrom(idOwningAction);
204213
}
@@ -207,7 +216,8 @@ private DbAction.WithEntity<?> getIdOwningAction(DbAction.WithEntity<?> action,
207216

208217
if (!(action instanceof DbAction.WithDependingOn<?> withDependingOn)) {
209218

210-
Assert.state(idPath.isRoot(), "When the id path is not empty the id providing action should be of type WithDependingOn");
219+
Assert.state(idPath.isRoot(),
220+
"When the id path is not empty the id providing action should be of type WithDependingOn");
211221

212222
return action;
213223
}
@@ -284,7 +294,9 @@ <T> List<T> populateIdsIfNecessary() {
284294
}
285295

286296
if (roots.isEmpty()) {
287-
throw new IllegalStateException(String.format("Cannot retrieve the resulting instance(s) unless a %s or %s action was successfully executed", DbAction.InsertRoot.class.getName(), DbAction.UpdateRoot.class.getName()));
297+
throw new IllegalStateException(
298+
String.format("Cannot retrieve the resulting instance(s) unless a %s or %s action was successfully executed",
299+
DbAction.InsertRoot.class.getName(), DbAction.UpdateRoot.class.getName()));
288300
}
289301

290302
Collections.reverse(roots);
@@ -293,19 +305,23 @@ <T> List<T> populateIdsIfNecessary() {
293305
}
294306

295307
@SuppressWarnings("unchecked")
296-
private <S> Object setIdAndCascadingProperties(DbAction.WithEntity<S> action, @Nullable Object generatedId, StagedValues cascadingValues) {
308+
private <S> Object setIdAndCascadingProperties(DbAction.WithEntity<S> action, @Nullable Object generatedId,
309+
StagedValues cascadingValues) {
297310

298311
S originalEntity = action.getEntity();
299312

300-
RelationalPersistentEntity<S> persistentEntity = (RelationalPersistentEntity<S>) context.getRequiredPersistentEntity(action.getEntityType());
301-
PersistentPropertyPathAccessor<S> propertyAccessor = converter.getPropertyAccessor(persistentEntity, originalEntity);
313+
RelationalPersistentEntity<S> persistentEntity = (RelationalPersistentEntity<S>) context
314+
.getRequiredPersistentEntity(action.getEntityType());
315+
PersistentPropertyPathAccessor<S> propertyAccessor = converter.getPropertyAccessor(persistentEntity,
316+
originalEntity);
302317

303318
if (IdValueSource.GENERATED.equals(action.getIdValueSource())) {
304319
propertyAccessor.setProperty(persistentEntity.getRequiredIdProperty(), generatedId);
305320
}
306321

307322
// set values of changed immutables referenced by this entity
308-
cascadingValues.forEachPath(action, (persistentPropertyPath, o) -> propertyAccessor.setProperty(getRelativePath(action, persistentPropertyPath), o));
323+
cascadingValues.forEachPath(action, (persistentPropertyPath, o) -> propertyAccessor
324+
.setProperty(getRelativePath(action, persistentPropertyPath), o));
309325

310326
return propertyAccessor.getBean();
311327
}
@@ -337,7 +353,8 @@ private <T> void updateWithoutVersion(DbAction.UpdateRoot<T> update) {
337353

338354
if (!accessStrategy.update(update.getEntity(), update.getEntityType())) {
339355

340-
throw new IncorrectUpdateSemanticsDataAccessException(String.format(UPDATE_FAILED, update.getEntity(), getIdFrom(update)));
356+
throw new IncorrectUpdateSemanticsDataAccessException(
357+
String.format(UPDATE_FAILED, update.getEntity(), getIdFrom(update)));
341358
}
342359
}
343360

@@ -358,20 +375,21 @@ private <T> void updateWithVersion(DbAction.UpdateRoot<T> update) {
358375
*/
359376
private static class StagedValues {
360377

361-
static final List<MultiValueAggregator<?>> aggregators = Arrays.asList(SetAggregator.INSTANCE, MapAggregator.INSTANCE, ListAggregator.INSTANCE, SingleElementAggregator.INSTANCE);
378+
static final List<MultiValueAggregator<?>> aggregators = Arrays.asList(SetAggregator.INSTANCE,
379+
MapAggregator.INSTANCE, ListAggregator.INSTANCE, SingleElementAggregator.INSTANCE);
362380

363381
Map<DbAction, Map<PersistentPropertyPath, StagedValue>> values = new HashMap<>();
364382

365383
/**
366384
* Adds a value that needs to be set in an entity higher up in the tree of entities in the aggregate. If the
367385
* attribute to be set is multivalued this method expects only a single element.
368386
*
369-
* @param action The action responsible for persisting the entity that needs the added value set. Must not be
370-
* {@literal null}.
371-
* @param path The path to the property in which to set the value. Must not be {@literal null}.
387+
* @param action The action responsible for persisting the entity that needs the added value set. Must not be
388+
* {@literal null}.
389+
* @param path The path to the property in which to set the value. Must not be {@literal null}.
372390
* @param qualifier If {@code path} is a qualified multivalued properties this parameter contains the qualifier. May
373-
* be {@literal null}.
374-
* @param value The value to be set. Must not be {@literal null}.
391+
* be {@literal null}.
392+
* @param value The value to be set. Must not be {@literal null}.
375393
*/
376394
void stage(DbAction<?> action, PersistentPropertyPath path, @Nullable Object qualifier, Object value) {
377395

@@ -384,9 +402,11 @@ <T> StagedValue gather(DbAction<?> action, PersistentPropertyPath path, @Nullabl
384402

385403
MultiValueAggregator<T> aggregator = getAggregatorFor(path);
386404

387-
Map<PersistentPropertyPath, StagedValue> valuesForPath = this.values.computeIfAbsent(action, dbAction -> new HashMap<>());
405+
Map<PersistentPropertyPath, StagedValue> valuesForPath = this.values.computeIfAbsent(action,
406+
dbAction -> new HashMap<>());
388407

389-
StagedValue stagedValue = valuesForPath.computeIfAbsent(path, persistentPropertyPath -> new StagedValue(aggregator.createEmptyInstance()));
408+
StagedValue stagedValue = valuesForPath.computeIfAbsent(path,
409+
persistentPropertyPath -> new StagedValue(aggregator.createEmptyInstance()));
390410
T currentValue = (T) stagedValue.value;
391411

392412
stagedValue.value = aggregator.add(currentValue, qualifier, value);
@@ -426,8 +446,7 @@ void forEachPath(DbAction<?> dbAction, BiConsumer<PersistentPropertyPath, Object
426446
}
427447

428448
private static class StagedValue {
429-
@Nullable
430-
Object value;
449+
@Nullable Object value;
431450
boolean isStaged;
432451

433452
public StagedValue(@Nullable Object value) {

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public static Identifier from(Map<SqlIdentifier, Object> map) {
106106
* @param identifier the identifier to append.
107107
* @return the {@link Identifier} containing all existing keys and the key part for {@code name}, {@code value}, and a
108108
* {@link Class target type}.
109-
* @since 3.5
109+
* @since 4.0
110110
*/
111111
public Identifier withPart(Identifier identifier) {
112112

@@ -207,7 +207,6 @@ public Object get(SqlIdentifier columnName) {
207207
return null;
208208
}
209209

210-
211210
/**
212211
* A single value of an Identifier consisting of the column name, the value and the target type which is to be used to
213212
* store the element in the database.

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

+14-13
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,24 @@ public static JdbcIdentifierBuilder empty() {
4242
/**
4343
* Creates ParentKeys with backreference for the given path and value of the parents id.
4444
*/
45-
// gets called during insert. value contains the id from an insert of the parent
46-
public static JdbcIdentifierBuilder forBackReferences(JdbcConverter converter, AggregatePath path, Function<AggregatePath, Object> valueProvider) {
45+
public static JdbcIdentifierBuilder forBackReferences(JdbcConverter converter, AggregatePath path,
46+
Function<AggregatePath, Object> valueProvider) {
4747

4848
return new JdbcIdentifierBuilder(forBackReference(converter, path, Identifier.empty(), valueProvider));
4949
}
5050

5151
/**
52-
* @param converter used for determining the column types to be used for different properties. Must not be {@literal null}.
53-
* @param path the path for which needs to back reference an id. Must not be {@literal null}.
54-
* @param defaultIdentifier Identifier to be used as a default when no backreference can be constructed. Must not be {@literal null}.
55-
* @param valueProvider provides values for the {@link Identifier} based on an {@link AggregatePath}. Must not be {@literal null}.
52+
* @param converter used for determining the column types to be used for different properties. Must not be
53+
* {@literal null}.
54+
* @param path the path for which needs to back reference an id. Must not be {@literal null}.
55+
* @param defaultIdentifier Identifier to be used as a default when no backreference can be constructed. Must not be
56+
* {@literal null}.
57+
* @param valueProvider provides values for the {@link Identifier} based on an {@link AggregatePath}. Must not be
58+
* {@literal null}.
5659
* @return Guaranteed not to be {@literal null}.
5760
*/
58-
public static Identifier forBackReference(JdbcConverter converter, AggregatePath path, Identifier defaultIdentifier, Function<AggregatePath, Object> valueProvider) {
61+
public static Identifier forBackReference(JdbcConverter converter, AggregatePath path, Identifier defaultIdentifier,
62+
Function<AggregatePath, Object> valueProvider) {
5963

6064
Identifier identifierToUse = defaultIdentifier;
6165

@@ -69,19 +73,17 @@ public static Identifier forBackReference(JdbcConverter converter, AggregatePath
6973
identifierToUse = infos.reduce(Identifier.empty(), (ap, ci) -> {
7074

7175
RelationalPersistentProperty property = ap.getRequiredLeafProperty();
72-
return Identifier.of(ci.name(), valueProvider.apply(ap),
73-
converter.getColumnType(property));
76+
return Identifier.of(ci.name(), valueProvider.apply(ap), converter.getColumnType(property));
7477
}, Identifier::withPart);
7578
}
7679

7780
return identifierToUse;
7881
}
7982

80-
8183
/**
8284
* Adds a qualifier to the identifier to build. A qualifier is a map key or a list index.
8385
*
84-
* @param path path to the map that gets qualified by {@code value}. Must not be {@literal null}.
86+
* @param path path to the map that gets qualified by {@code value}. Must not be {@literal null}.
8587
* @param value map key or list index qualifying the map identified by {@code path}. Must not be {@literal null}.
8688
* @return this builder. Guaranteed to be not {@literal null}.
8789
*/
@@ -91,8 +93,7 @@ public JdbcIdentifierBuilder withQualifier(AggregatePath path, Object value) {
9193
Assert.notNull(value, "Value must not be null");
9294

9395
AggregatePath.TableInfo tableInfo = path.getTableInfo();
94-
identifier = identifier.withPart(tableInfo.qualifierColumnInfo().name(), value,
95-
tableInfo.qualifierColumnType());
96+
identifier = identifier.withPart(tableInfo.qualifierColumnInfo().name(), value, tableInfo.qualifierColumnType());
9697

9798
return this;
9899
}

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

+14-13
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public class MappingJdbcConverter extends MappingRelationalConverter implements
8080
* {@link #MappingJdbcConverter(RelationalMappingContext, RelationResolver, CustomConversions, JdbcTypeFactory)}
8181
* (MappingContext, RelationResolver, JdbcTypeFactory)} to convert arrays and large objects into JDBC-specific types.
8282
*
83-
* @param context must not be {@literal null}.
83+
* @param context must not be {@literal null}.
8484
* @param relationResolver used to fetch additional relations from the database. Must not be {@literal null}.
8585
*/
8686
public MappingJdbcConverter(RelationalMappingContext context, RelationResolver relationResolver) {
@@ -98,12 +98,12 @@ public MappingJdbcConverter(RelationalMappingContext context, RelationResolver r
9898
/**
9999
* Creates a new {@link MappingJdbcConverter} given {@link MappingContext}.
100100
*
101-
* @param context must not be {@literal null}.
101+
* @param context must not be {@literal null}.
102102
* @param relationResolver used to fetch additional relations from the database. Must not be {@literal null}.
103-
* @param typeFactory must not be {@literal null}
103+
* @param typeFactory must not be {@literal null}
104104
*/
105105
public MappingJdbcConverter(RelationalMappingContext context, RelationResolver relationResolver,
106-
CustomConversions conversions, JdbcTypeFactory typeFactory) {
106+
CustomConversions conversions, JdbcTypeFactory typeFactory) {
107107

108108
super(context, conversions);
109109

@@ -285,7 +285,7 @@ public <R> R readAndResolve(TypeInformation<R> type, RowDocument source, Identif
285285

286286
@Override
287287
protected RelationalPropertyValueProvider newValueProvider(RowDocumentAccessor documentAccessor,
288-
ValueExpressionEvaluator evaluator, ConversionContext context) {
288+
ValueExpressionEvaluator evaluator, ConversionContext context) {
289289

290290
if (context instanceof ResolvingConversionContext rcc) {
291291

@@ -314,7 +314,7 @@ class ResolvingRelationalPropertyValueProvider implements RelationalPropertyValu
314314
private final Identifier identifier;
315315

316316
private ResolvingRelationalPropertyValueProvider(AggregatePathValueProvider delegate, RowDocumentAccessor accessor,
317-
ResolvingConversionContext context, Identifier identifier) {
317+
ResolvingConversionContext context, Identifier identifier) {
318318

319319
AggregatePath path = context.aggregatePath();
320320

@@ -323,15 +323,15 @@ private ResolvingRelationalPropertyValueProvider(AggregatePathValueProvider dele
323323
this.context = context;
324324
this.identifier = path.isEntity()
325325
? potentiallyAppendIdentifier(identifier, path.getRequiredLeafEntity(),
326-
property -> delegate.getValue(path.append(property)))
326+
property -> delegate.getValue(path.append(property)))
327327
: identifier;
328328
}
329329

330330
/**
331331
* Conditionally append the identifier if the entity has an identifier property.
332332
*/
333333
static Identifier potentiallyAppendIdentifier(Identifier base, RelationalPersistentEntity<?> entity,
334-
Function<RelationalPersistentProperty, Object> getter) {
334+
Function<RelationalPersistentProperty, Object> getter) {
335335

336336
if (entity.hasIdProperty()) {
337337

@@ -361,7 +361,8 @@ public <T> T getPropertyValue(RelationalPersistentProperty property) {
361361

362362
if (property.isCollectionLike() || property.isMap()) {
363363

364-
Identifier identifier = JdbcIdentifierBuilder.forBackReference(MappingJdbcConverter.this, aggregatePath, this.identifier, getWrappedValueProvider(delegate::getValue, aggregatePath));
364+
Identifier identifier = JdbcIdentifierBuilder.forBackReference(MappingJdbcConverter.this, aggregatePath,
365+
this.identifier, getWrappedValueProvider(delegate::getValue, aggregatePath));
365366

366367
Iterable<Object> allByPath = relationResolver.findAllByPath(identifier,
367368
aggregatePath.getRequiredPersistentPropertyPath());
@@ -445,11 +446,12 @@ public RelationalPropertyValueProvider withContext(ConversionContext context) {
445446

446447
return context == this.context ? this
447448
: new ResolvingRelationalPropertyValueProvider(delegate.withContext(context), accessor,
448-
(ResolvingConversionContext) context, identifier);
449+
(ResolvingConversionContext) context, identifier);
449450
}
450451
}
451452

452-
private static Function<AggregatePath, Object> getWrappedValueProvider(Function<AggregatePath, Object> valueProvider, AggregatePath aggregatePath) {
453+
private static Function<AggregatePath, Object> getWrappedValueProvider(Function<AggregatePath, Object> valueProvider,
454+
AggregatePath aggregatePath) {
453455

454456
AggregatePath idDefiningParentPath = aggregatePath.getIdDefiningParentPath();
455457

@@ -464,7 +466,6 @@ private static Function<AggregatePath, Object> getWrappedValueProvider(Function<
464466
return ap -> valueProvider.apply(idPath.append(ap));
465467
}
466468

467-
468469
/**
469470
* Marker object to indicate that the property value provider should resolve relations.
470471
*
@@ -473,7 +474,7 @@ private static Function<AggregatePath, Object> getWrappedValueProvider(Function<
473474
* @param identifier
474475
*/
475476
private record ResolvingConversionContext(ConversionContext delegate, AggregatePath aggregatePath,
476-
Identifier identifier) implements ConversionContext {
477+
Identifier identifier) implements ConversionContext {
477478

478479
@Override
479480
public <S> S convert(Object source, TypeInformation<? extends S> typeHint) {

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

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ Column getColumn(AggregatePath path) {
6666
*
6767
* @param path must not be null.
6868
* @return a {@literal Column} that is part of the effective primary key for the given path.
69+
* @since 4.0
6970
*/
7071
Column getAnyReverseColumn(AggregatePath path) {
7172

0 commit comments

Comments
 (0)