Skip to content

Commit 555944d

Browse files
committed
AggregatePath caching consideres the owner.
Since a PersistencePropertyPath does NOT consider it's owner for equality, this is necessary. to distinguish different AggregatePath instances based on a inherited property. Closes #1657
1 parent 156cccc commit 555944d

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class DefaultAggregatePath implements AggregatePath {
5252

5353
this.context = context;
5454
this.path = (PersistentPropertyPath) path;
55-
this.rootType = null;
55+
this.rootType = path.getBaseProperty().getOwner();
5656
}
5757

5858
DefaultAggregatePath(RelationalMappingContext context, RelationalPersistentEntity<?> rootType) {

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,25 +186,40 @@ protected void applyDefaults(BasicRelationalPersistentProperty persistentPropert
186186
*/
187187
public AggregatePath getAggregatePath(PersistentPropertyPath<? extends RelationalPersistentProperty> path) {
188188

189-
AggregatePath aggregatePath = aggregatePathCache.get(path);
189+
AggregatePathCacheKey cacheKey = AggregatePathCacheKey.of(path);
190+
191+
AggregatePath aggregatePath = aggregatePathCache.get(cacheKey);
190192
if (aggregatePath == null) {
191193

192194
aggregatePath = new DefaultAggregatePath(this, path);
193-
aggregatePathCache.put(path, aggregatePath);
195+
aggregatePathCache.put(cacheKey, aggregatePath);
194196
}
195197

196198
return aggregatePath;
197199
}
198200

199201
public AggregatePath getAggregatePath(RelationalPersistentEntity<?> type) {
200202

201-
AggregatePath aggregatePath = aggregatePathCache.get(type);
203+
AggregatePathCacheKey cacheKey = AggregatePathCacheKey.of(type);
204+
205+
AggregatePath aggregatePath = aggregatePathCache.get(cacheKey);
202206
if (aggregatePath == null) {
203207

204208
aggregatePath = new DefaultAggregatePath(this, type);
205-
aggregatePathCache.put(type, aggregatePath);
209+
aggregatePathCache.put(cacheKey, aggregatePath);
206210
}
207211

208212
return aggregatePath;
209213
}
214+
215+
private record AggregatePathCacheKey(RelationalPersistentEntity<?> root,@Nullable PersistentPropertyPath<? extends RelationalPersistentProperty> path) {
216+
static AggregatePathCacheKey of(RelationalPersistentEntity<?> root) {
217+
return new AggregatePathCacheKey(root, null);
218+
}
219+
static AggregatePathCacheKey of(PersistentPropertyPath<? extends RelationalPersistentProperty> path) {
220+
221+
RelationalPersistentEntity<?> root = path.getBaseProperty().getOwner();
222+
return new AggregatePathCacheKey(root, path);
223+
}
224+
}
210225
}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,20 @@ void correctlyCascadesPrefix() {
104104
assertThat(name.getColumnName()).isEqualTo(SqlIdentifier.quoted("PRNT_CHLD_NAME"));
105105
}
106106

107+
@Test // GH-1657
108+
void aggregatePathsOfBasePropertyForDifferentInheritedEntitiesAreDifferent() {
109+
110+
PersistentPropertyPath<RelationalPersistentProperty> path1 = context.getPersistentPropertyPath("name",
111+
Inherit1.class);
112+
PersistentPropertyPath<RelationalPersistentProperty> path2 = context.getPersistentPropertyPath("name",
113+
Inherit2.class);
114+
115+
AggregatePath aggregatePath1 = context.getAggregatePath(path1);
116+
AggregatePath aggregatePath2 = context.getAggregatePath(path2);
117+
118+
assertThat(aggregatePath1).isNotEqualTo(aggregatePath2);
119+
}
120+
107121
static class EntityWithUuid {
108122
@Id UUID uuid;
109123
}
@@ -121,4 +135,12 @@ static class Child {
121135
String name;
122136
}
123137

138+
static class Base {
139+
String name;
140+
}
141+
142+
static class Inherit1 extends Base {}
143+
144+
static class Inherit2 extends Base {}
145+
124146
}

0 commit comments

Comments
 (0)