Skip to content

Commit 81aa5c8

Browse files
committed
Processing of Review comments.
Using isRoot() instead of path == null. Rename extendBy to append. Move getRequiredLeafEntity closer to getLeafEntity. Replace matches with equals.
1 parent b1b5a3a commit 81aa5c8

File tree

6 files changed

+75
-82
lines changed

6 files changed

+75
-82
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ private DbAction.WithEntity<?> getIdOwningAction(DbAction.WithEntity<?> action,
203203
return action;
204204
}
205205

206-
if (idPath.matches(withDependingOn.getPropertyPath())) {
206+
if (idPath.equals(context.getAggregatePath(withDependingOn.getPropertyPath()))) {
207207
return action;
208208
}
209209

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ private ReadingContext(RelationalPersistentEntity<T> entity, AggregatePath rootP
396396
private <S> ReadingContext<S> extendBy(RelationalPersistentProperty property) {
397397
return new ReadingContext<>(
398398
(RelationalPersistentEntity<S>) getMappingContext().getRequiredPersistentEntity(property.getActualType()),
399-
rootPath.extendBy(property), path.extendBy(property), identifier, key,
399+
rootPath.append(property), path.append(property), identifier, key,
400400
propertyValueProvider.extendBy(property), backReferencePropertyValueProvider.extendBy(property), accessor);
401401
}
402402

@@ -457,9 +457,9 @@ private Iterable<Object> resolveRelation(@Nullable Object id, RelationalPersiste
457457

458458
Identifier identifier = id == null //
459459
? this.identifier.withPart(rootPath.getQualifierColumn(), key, Object.class) //
460-
: Identifier.of(rootPath.extendBy(property).getReverseColumnName(), id, Object.class);
460+
: Identifier.of(rootPath.append(property).getReverseColumnName(), id, Object.class);
461461

462-
PersistentPropertyPath<? extends RelationalPersistentProperty> propertyPath = path.extendBy(property)
462+
PersistentPropertyPath<? extends RelationalPersistentProperty> propertyPath = path.append(property)
463463
.getRequiredPersistentPropertyPath();
464464

465465
return relationResolver.findAllByPath(identifier, propertyPath);

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

+2-4
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@
1717

1818
import org.springframework.data.mapping.model.PropertyValueProvider;
1919
import org.springframework.data.relational.core.mapping.AggregatePath;
20-
import org.springframework.data.relational.core.mapping.PersistentPropertyPathExtension;
2120
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
22-
import org.springframework.data.relational.core.sql.IdentifierProcessing;
2321

2422
/**
2523
* {@link PropertyValueProvider} obtaining values from a {@link ResultSetAccessor}. For a given id property it provides
@@ -47,10 +45,10 @@ class JdbcBackReferencePropertyValueProvider implements PropertyValueProvider<Re
4745

4846
@Override
4947
public <T> T getPropertyValue(RelationalPersistentProperty property) {
50-
return (T) resultSet.getObject(basePath.extendBy(property).getReverseColumnNameAlias().getReference());
48+
return (T) resultSet.getObject(basePath.append(property).getReverseColumnNameAlias().getReference());
5149
}
5250

5351
public JdbcBackReferencePropertyValueProvider extendBy(RelationalPersistentProperty property) {
54-
return new JdbcBackReferencePropertyValueProvider(basePath.extendBy(property), resultSet);
52+
return new JdbcBackReferencePropertyValueProvider(basePath.append(property), resultSet);
5553
}
5654
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ public boolean hasProperty(RelationalPersistentProperty property) {
5757
}
5858

5959
private String getColumnName(RelationalPersistentProperty property) {
60-
return basePath.extendBy(property).getColumnAlias().getReference();
60+
return basePath.append(property).getColumnAlias().getReference();
6161
}
6262

6363
public JdbcPropertyValueProvider extendBy(RelationalPersistentProperty property) {
64-
return new JdbcPropertyValueProvider(basePath.extendBy(property), resultSet);
64+
return new JdbcPropertyValueProvider(basePath.append(property), resultSet);
6565
}
6666
}

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

+65-58
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import org.springframework.util.Assert;
2424
import org.springframework.util.StringUtils;
2525

26+
import java.util.Objects;
27+
2628
/**
2729
* Represents a path within an aggregate starting from the aggregate root.
2830
*
@@ -69,24 +71,14 @@ public boolean isRoot() {
6971
return path == null;
7072
}
7173

72-
/**
73-
* Tests if {@code this} and the argument represent the same path.
74-
*
75-
* @param path to which this path gets compared. May be {@literal null}.
76-
* @return Whence the argument matches the path represented by this instance.
77-
*/
78-
public boolean matches(@Nullable PersistentPropertyPath<RelationalPersistentProperty> path) {
79-
return this.path == null ? path == null || path.isEmpty() : this.path.equals(path);
80-
}
81-
8274
/**
8375
* The name of the column used to reference the id in the parent table.
8476
*
8577
* @throws IllegalStateException when called on an empty path.
8678
*/
8779
public SqlIdentifier getReverseColumnName() {
8880

89-
Assert.state(path != null, "Empty paths don't have a reverse column name");
81+
Assert.state(!isRoot(), "Empty paths don't have a reverse column name");
9082

9183
return path.getLeafProperty().getReverseColumnName(this);
9284
}
@@ -99,7 +91,7 @@ public SqlIdentifier getReverseColumnName() {
9991
*/
10092
public AggregatePath getParentPath() {
10193

102-
if (path == null) {
94+
if (isRoot()) {
10395
throw new IllegalStateException("The parent path of a root path is not defined.");
10496
}
10597

@@ -117,7 +109,27 @@ public AggregatePath getParentPath() {
117109
*/
118110
@Nullable
119111
public RelationalPersistentEntity<?> getLeafEntity() {
120-
return path == null ? rootType : context.getPersistentEntity(path.getLeafProperty().getActualType());
112+
return isRoot() ? rootType : context.getPersistentEntity(path.getLeafProperty().getActualType());
113+
}
114+
115+
/**
116+
* The {@link RelationalPersistentEntity} associated with the leaf of this path or throw {@link IllegalStateException}
117+
* if the leaf cannot be resolved.
118+
*
119+
* @return the required {@link RelationalPersistentEntity} associated with the leaf of this path.
120+
* @throws IllegalStateException if the persistent entity cannot be resolved.
121+
*/
122+
public RelationalPersistentEntity<?> getRequiredLeafEntity() {
123+
124+
RelationalPersistentEntity<?> entity = getLeafEntity();
125+
126+
if (entity == null) {
127+
128+
throw new IllegalStateException(
129+
String.format("Couldn't resolve leaf PersistentEntity for type %s", path.getLeafProperty().getActualType()));
130+
}
131+
132+
return entity;
121133
}
122134

123135
/**
@@ -138,7 +150,7 @@ public AggregatePath getIdDefiningParentPath() {
138150

139151
AggregatePath parent = getParentPath();
140152

141-
if (parent.path == null) {
153+
if (isRoot()) {
142154
return parent;
143155
}
144156

@@ -149,33 +161,13 @@ public AggregatePath getIdDefiningParentPath() {
149161
return parent;
150162
}
151163

152-
/**
153-
* The {@link RelationalPersistentEntity} associated with the leaf of this path or throw {@link IllegalStateException}
154-
* if the leaf cannot be resolved.
155-
*
156-
* @return the required {@link RelationalPersistentEntity} associated with the leaf of this path.
157-
* @throws IllegalStateException if the persistent entity cannot be resolved.
158-
*/
159-
public RelationalPersistentEntity<?> getRequiredLeafEntity() {
160-
161-
RelationalPersistentEntity<?> entity = getLeafEntity();
162-
163-
if (entity == null) {
164-
165-
throw new IllegalStateException(
166-
String.format("Couldn't resolve leaf PersistentEntity for type %s", path.getLeafProperty().getActualType()));
167-
}
168-
169-
return entity;
170-
}
171-
172164
public RelationalPersistentProperty getRequiredIdProperty() {
173-
return this.path == null ? rootType.getRequiredIdProperty() : getRequiredLeafEntity().getRequiredIdProperty();
165+
return isRoot() ? rootType.getRequiredIdProperty() : getRequiredLeafEntity().getRequiredIdProperty();
174166

175167
}
176168

177169
public int getLength() {
178-
return path == null ? 0 : path.getLength();
170+
return isRoot() ? 0 : path.getLength();
179171
}
180172

181173
/**
@@ -185,7 +177,7 @@ public int getLength() {
185177
*/
186178
@Nullable
187179
public SqlIdentifier getQualifierColumn() {
188-
return path == null ? null : path.getLeafProperty().getKeyColumn();
180+
return isRoot() ? null : path.getLeafProperty().getKeyColumn();
189181
}
190182

191183
/**
@@ -196,7 +188,7 @@ public SqlIdentifier getQualifierColumn() {
196188
@Nullable
197189
public Class<?> getQualifierColumnType() {
198190

199-
if (path == null) {
191+
if (isRoot()) {
200192
return null;
201193
}
202194
if (!path.getLeafProperty().isQualified()) {
@@ -211,14 +203,14 @@ public Class<?> getQualifierColumnType() {
211203
* @return if the leaf property is embedded.
212204
*/
213205
public boolean isEmbedded() {
214-
return path != null && path.getLeafProperty().isEmbedded();
206+
return !isRoot() && path.getLeafProperty().isEmbedded();
215207
}
216208

217209
/**
218210
* @return {@literal true} when this is an empty path or the path references an entity.
219211
*/
220212
public boolean isEntity() {
221-
return path == null || path.getLeafProperty().isEntity();
213+
return isRoot() || path.getLeafProperty().isEntity();
222214
}
223215

224216
/**
@@ -235,7 +227,7 @@ private AggregatePath getTableOwningAncestor() {
235227
@Nullable
236228
private SqlIdentifier assembleTableAlias() {
237229

238-
Assert.state(path != null, "Path is null");
230+
Assert.state(!isRoot(), "Path is null");
239231

240232
RelationalPersistentProperty leafProperty = path.getLeafProperty();
241233
String prefix;
@@ -273,7 +265,7 @@ public SqlIdentifier getTableAlias() {
273265

274266
AggregatePath tableOwner = getTableOwningAncestor();
275267

276-
return tableOwner.path == null ? null : tableOwner.assembleTableAlias();
268+
return tableOwner.isRoot() ? null : tableOwner.assembleTableAlias();
277269

278270
}
279271

@@ -295,7 +287,7 @@ public SqlIdentifier getQualifiedTableName() {
295287
*/
296288
public SqlIdentifier getColumnName() {
297289

298-
Assert.state(path != null, "Path is null");
290+
Assert.state(!isRoot(), "Path is null");
299291

300292
return assembleColumnName(path.getLeafProperty().getColumnName());
301293
}
@@ -323,12 +315,12 @@ public SqlIdentifier getReverseColumnNameAlias() {
323315
public String toString() {
324316
return "AggregatePath["
325317
+ (rootType == null ? path.getBaseProperty().getOwner().getType().getName() : rootType.getName()) + "]"
326-
+ ((path == null) ? "/" : path.toDotPath());
318+
+ ((isRoot()) ? "/" : path.toDotPath());
327319
}
328320

329321
private SqlIdentifier assembleColumnName(SqlIdentifier suffix) {
330322

331-
Assert.state(path != null, "Path is null");
323+
Assert.state(!isRoot(), "Path is null");
332324

333325
if (path.getLength() <= 1) {
334326
return suffix;
@@ -353,7 +345,7 @@ private SqlIdentifier prefixWithTableAlias(SqlIdentifier columnName) {
353345
}
354346

355347
public String toDotPath() {
356-
return path == null ? "" : path.toDotPath();
348+
return isRoot() ? "" : path.toDotPath();
357349
}
358350

359351
/**
@@ -364,7 +356,7 @@ public String toDotPath() {
364356
*/
365357
public boolean isMultiValued() {
366358

367-
return path != null && //
359+
return !isRoot() && //
368360
(path.getLeafProperty().isCollectionLike() //
369361
|| path.getLeafProperty().isQualified() //
370362
|| getParentPath().isMultiValued() //
@@ -376,27 +368,27 @@ public boolean isMultiValued() {
376368
* @see RelationalPersistentProperty#isMap()
377369
*/
378370
public boolean isMap() {
379-
return path != null && path.getLeafProperty().isMap();
371+
return !isRoot() && path.getLeafProperty().isMap();
380372
}
381373

382374
/**
383375
* @return {@literal true} when this is references a {@link java.util.List} or {@link java.util.Map}.
384376
*/
385377
public boolean isQualified() {
386-
return path != null && path.getLeafProperty().isQualified();
378+
return !isRoot() && path.getLeafProperty().isQualified();
387379
}
388380

389381
public RelationalPersistentProperty getRequiredLeafProperty() {
390382

391-
if (path == null) {
383+
if (isRoot()) {
392384
throw new IllegalStateException("root path does not have a leaf property");
393385
}
394386
return path.getLeafProperty();
395387
}
396388

397389
public RelationalPersistentProperty getBaseProperty() {
398390

399-
if (path == null) {
391+
if (isRoot()) {
400392
throw new IllegalStateException("root path does not have a base property");
401393
}
402394
return path.getBaseProperty();
@@ -413,15 +405,15 @@ public SqlIdentifier getIdColumnName() {
413405
* @return {@literal true} when this is references a {@link java.util.Collection} or an array.
414406
*/
415407
public boolean isCollectionLike() {
416-
return path != null && path.getLeafProperty().isCollectionLike();
408+
return !isRoot() && path.getLeafProperty().isCollectionLike();
417409
}
418410

419411
/**
420412
* @return whether the leaf end of the path is ordered, i.e. the data to populate must be ordered.
421413
* @see RelationalPersistentProperty#isOrdered()
422414
*/
423415
public boolean isOrdered() {
424-
return path != null && path.getLeafProperty().isOrdered();
416+
return !isRoot() && path.getLeafProperty().isOrdered();
425417
}
426418

427419
/**
@@ -430,9 +422,9 @@ public boolean isOrdered() {
430422
* @param property must not be {@literal null}.
431423
* @return Guaranteed to be not {@literal null}.
432424
*/
433-
public AggregatePath extendBy(RelationalPersistentProperty property) {
425+
public AggregatePath append(RelationalPersistentProperty property) {
434426

435-
PersistentPropertyPath<? extends RelationalPersistentProperty> newPath = path == null //
427+
PersistentPropertyPath<? extends RelationalPersistentProperty> newPath = isRoot() //
436428
? context.getPersistentPropertyPath(property.getName(), rootType.getType()) //
437429
: context.getPersistentPropertyPath(path.toDotPath() + "." + property.getName(),
438430
path.getBaseProperty().getOwner().getType());
@@ -442,7 +434,7 @@ public AggregatePath extendBy(RelationalPersistentProperty property) {
442434

443435
public PersistentPropertyPath<? extends RelationalPersistentProperty> getRequiredPersistentPropertyPath() {
444436

445-
Assert.state(path != null, "path must not be null");
437+
Assert.state(!isRoot(), "path must not be null");
446438
return path;
447439
}
448440

@@ -453,15 +445,30 @@ public PersistentPropertyPath<? extends RelationalPersistentProperty> getRequire
453445
public SqlIdentifier getEffectiveIdColumnName() {
454446

455447
AggregatePath owner = getTableOwningAncestor();
456-
return owner.path == null ? owner.getRequiredLeafEntity().getIdColumn() : owner.getReverseColumnName();
448+
return owner.isRoot() ? owner.getRequiredLeafEntity().getIdColumn() : owner.getReverseColumnName();
457449
}
458450

459451
@Nullable
460452
public PersistentPropertyPathExtension getPathExtension() {
461453

462-
if (path == null) {
454+
if (isRoot()) {
463455
return new PersistentPropertyPathExtension(context, rootType);
464456
}
465457
return new PersistentPropertyPathExtension(context, path);
466458
}
459+
460+
@Override
461+
public boolean equals(Object o) {
462+
463+
if (this == o) return true;
464+
if (o == null || getClass() != o.getClass()) return false;
465+
AggregatePath that = (AggregatePath) o;
466+
return Objects.equals(context, that.context) && Objects.equals(rootType, that.rootType) && Objects.equals(path, that.path);
467+
}
468+
469+
@Override
470+
public int hashCode() {
471+
472+
return Objects.hash(context, rootType, path);
473+
}
467474
}

0 commit comments

Comments
 (0)