diff --git a/spring-data-relational/src/main/java/org/springframework/data/relational/core/mapping/DefaultAggregatePath.java b/spring-data-relational/src/main/java/org/springframework/data/relational/core/mapping/DefaultAggregatePath.java index 015857e9..0c397562 100644 --- a/spring-data-relational/src/main/java/org/springframework/data/relational/core/mapping/DefaultAggregatePath.java +++ b/spring-data-relational/src/main/java/org/springframework/data/relational/core/mapping/DefaultAggregatePath.java @@ -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) { diff --git a/spring-data-relational/src/main/java/org/springframework/data/relational/core/mapping/RelationalMappingContext.java b/spring-data-relational/src/main/java/org/springframework/data/relational/core/mapping/RelationalMappingContext.java index ac453b4a..10c66cdc 100644 --- a/spring-data-relational/src/main/java/org/springframework/data/relational/core/mapping/RelationalMappingContext.java +++ b/spring-data-relational/src/main/java/org/springframework/data/relational/core/mapping/RelationalMappingContext.java @@ -186,11 +186,13 @@ public class RelationalMappingContext */ public AggregatePath getAggregatePath(PersistentPropertyPath 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 path) { + static AggregatePathCacheKey of(RelationalPersistentEntity root) { + return new AggregatePathCacheKey(root, null); + } + static AggregatePathCacheKey of(PersistentPropertyPath path) { + + RelationalPersistentEntity root = path.getBaseProperty().getOwner(); + return new AggregatePathCacheKey(root, path); + } + } } diff --git a/spring-data-relational/src/test/java/org/springframework/data/relational/core/mapping/RelationalMappingContextUnitTests.java b/spring-data-relational/src/test/java/org/springframework/data/relational/core/mapping/RelationalMappingContextUnitTests.java index c0377fdc..ee7e3ce2 100644 --- a/spring-data-relational/src/test/java/org/springframework/data/relational/core/mapping/RelationalMappingContextUnitTests.java +++ b/spring-data-relational/src/test/java/org/springframework/data/relational/core/mapping/RelationalMappingContextUnitTests.java @@ -104,6 +104,20 @@ public class RelationalMappingContextUnitTests { assertThat(name.getColumnName()).isEqualTo(SqlIdentifier.quoted("PRNT_CHLD_NAME")); } + @Test // GH-1657 + void aggregatePathsOfBasePropertyForDifferentInheritedEntitiesAreDifferent() { + + PersistentPropertyPath path1 = context.getPersistentPropertyPath("name", + Inherit1.class); + PersistentPropertyPath 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 {} + }