AggregatePath caching considers property 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
Original pull request: #1661
This commit is contained in:
Jens Schauder
2023-11-13 09:51:12 +01:00
committed by Mark Paluch
parent 99e51422fd
commit eae39d4438
3 changed files with 42 additions and 5 deletions

View File

@@ -52,7 +52,7 @@ class DefaultAggregatePath implements AggregatePath {
this.context = context;
this.path = (PersistentPropertyPath) path;
this.rootType = null;
this.rootType = path.getBaseProperty().getOwner();
}
DefaultAggregatePath(RelationalMappingContext context, RelationalPersistentEntity<?> rootType) {

View File

@@ -186,11 +186,13 @@ public class RelationalMappingContext
*/
public AggregatePath getAggregatePath(PersistentPropertyPath<? extends RelationalPersistentProperty> path) {
AggregatePath aggregatePath = aggregatePathCache.get(path);
AggregatePathCacheKey cacheKey = AggregatePathCacheKey.of(path);
AggregatePath aggregatePath = aggregatePathCache.get(cacheKey);
if (aggregatePath == null) {
aggregatePath = new DefaultAggregatePath(this, path);
aggregatePathCache.put(path, aggregatePath);
aggregatePathCache.put(cacheKey, aggregatePath);
}
return aggregatePath;
@@ -198,13 +200,26 @@ public class RelationalMappingContext
public AggregatePath getAggregatePath(RelationalPersistentEntity<?> type) {
AggregatePath aggregatePath = aggregatePathCache.get(type);
AggregatePathCacheKey cacheKey = AggregatePathCacheKey.of(type);
AggregatePath aggregatePath = aggregatePathCache.get(cacheKey);
if (aggregatePath == null) {
aggregatePath = new DefaultAggregatePath(this, type);
aggregatePathCache.put(type, aggregatePath);
aggregatePathCache.put(cacheKey, aggregatePath);
}
return aggregatePath;
}
private record AggregatePathCacheKey(RelationalPersistentEntity<?> root,@Nullable PersistentPropertyPath<? extends RelationalPersistentProperty> path) {
static AggregatePathCacheKey of(RelationalPersistentEntity<?> root) {
return new AggregatePathCacheKey(root, null);
}
static AggregatePathCacheKey of(PersistentPropertyPath<? extends RelationalPersistentProperty> path) {
RelationalPersistentEntity<?> root = path.getBaseProperty().getOwner();
return new AggregatePathCacheKey(root, path);
}
}
}

View File

@@ -104,6 +104,20 @@ public class RelationalMappingContextUnitTests {
assertThat(name.getColumnName()).isEqualTo(SqlIdentifier.quoted("PRNT_CHLD_NAME"));
}
@Test // GH-1657
void aggregatePathsOfBasePropertyForDifferentInheritedEntitiesAreDifferent() {
PersistentPropertyPath<RelationalPersistentProperty> path1 = context.getPersistentPropertyPath("name",
Inherit1.class);
PersistentPropertyPath<RelationalPersistentProperty> path2 = context.getPersistentPropertyPath("name",
Inherit2.class);
AggregatePath aggregatePath1 = context.getAggregatePath(path1);
AggregatePath aggregatePath2 = context.getAggregatePath(path2);
assertThat(aggregatePath1).isNotEqualTo(aggregatePath2);
}
static class EntityWithUuid {
@Id UUID uuid;
}
@@ -121,4 +135,12 @@ public class RelationalMappingContextUnitTests {
String name;
}
static class Base {
String name;
}
static class Inherit1 extends Base {}
static class Inherit2 extends Base {}
}