Skip to content

Commit 93913b0

Browse files
committed
AbstractPersistentProperty now considers the owner for equals.
This makes a difference when a property is declared in a superclass of two entities. In such a case the property is the same, but the owner is different. Closes #2972 Original pull request #2973 See spring-projects/spring-data-relational#1657
1 parent 6bd28a1 commit 93913b0

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.lang.reflect.Modifier;
2121
import java.util.Collections;
2222
import java.util.HashSet;
23+
import java.util.Objects;
2324
import java.util.Optional;
2425
import java.util.Set;
2526
import java.util.stream.Collectors;
@@ -40,6 +41,7 @@
4041
* @author Oliver Gierke
4142
* @author Christoph Strobl
4243
* @author Mark Paluch
44+
* @author Jens Schauder
4345
*/
4446
public abstract class AbstractPersistentProperty<P extends PersistentProperty<P>> implements PersistentProperty<P> {
4547

@@ -85,7 +87,7 @@ public AbstractPersistentProperty(Property property, PersistentEntity<?, P> owne
8587
this.association = Lazy.of(() -> isAssociation() ? createAssociation() : null);
8688
this.owner = owner;
8789

88-
this.hashCode = Lazy.of(property::hashCode);
90+
this.hashCode = Lazy.of(() -> Objects.hash(property, owner));
8991
this.usePropertyAccess = Lazy.of(() -> owner.getType().isInterface() || CAUSE_FIELD.equals(getField()));
9092

9193
this.isAssociation = Lazy.of(() -> ASSOCIATION_TYPE != null && ASSOCIATION_TYPE.isAssignableFrom(rawType));
@@ -293,7 +295,7 @@ public boolean equals(@Nullable Object obj) {
293295
return false;
294296
}
295297

296-
return this.property.equals(that.property);
298+
return this.property.equals(that.property) && this.owner.equals(that.owner);
297299
}
298300

299301
@Override

src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,6 @@ void isEntityWorksForUntypedCollection() {
8484
assertThat(getProperty(TestClassComplex.class, "collection").isEntity()).isFalse();
8585
}
8686

87-
@Test // DATACMNS-121
88-
void considersPropertiesEqualIfFieldEquals() {
89-
90-
var firstProperty = getProperty(FirstConcrete.class, "genericField");
91-
var secondProperty = getProperty(SecondConcrete.class, "genericField");
92-
93-
assertThat(firstProperty).isEqualTo(secondProperty);
94-
assertThat(firstProperty.hashCode()).isEqualTo(secondProperty.hashCode());
95-
}
96-
9787
@Test // DATACMNS-180
9888
void doesNotConsiderJavaTransientFieldsTransient() {
9989
assertThat(getProperty(TestClassComplex.class, "transientField").isTransient()).isFalse();
@@ -210,7 +200,7 @@ void resolvesFieldNameWithUnderscoresCorrectly() {
210200
@Test // DATACMNS-1139
211201
void resolvesGenericsForRawType() {
212202

213-
var property = getProperty(FirstConcrete.class, "genericField");
203+
var property = getProperty(Concrete.class, "genericField");
214204

215205
assertThat(property.getRawType()).isEqualTo(String.class);
216206
}
@@ -243,6 +233,15 @@ void considersVavrMaps() {
243233
assertThat(property.isMap()).isTrue();
244234
}
245235

236+
@Test // GH-2972
237+
void equalsConsidersOwner() {
238+
239+
SamplePersistentProperty id1 = getProperty(Inherited1.class, "id");
240+
SamplePersistentProperty id2 = getProperty(Inherited2.class, "id");
241+
242+
assertThat(id1).isNotEqualTo(id2);
243+
}
244+
246245
private <T> BasicPersistentEntity<T, SamplePersistentProperty> getEntity(Class<T> type) {
247246
return new BasicPersistentEntity<>(TypeInformation.of(type));
248247
}
@@ -280,11 +279,7 @@ class Generic<T> {
280279

281280
}
282281

283-
class FirstConcrete extends Generic<String> {
284-
285-
}
286-
287-
class SecondConcrete extends Generic<Integer> {
282+
class Concrete extends Generic<String> {
288283

289284
}
290285

@@ -409,4 +404,15 @@ interface JMoleculesAggregate extends AggregateRoot<JMoleculesAggregate, Identif
409404
class VavrWrapper {
410405
io.vavr.collection.Map<String, String> vavrMap;
411406
}
407+
408+
class Base {
409+
Long id;
410+
}
411+
412+
class Inherited1 extends Base {
413+
}
414+
415+
class Inherited2 extends Base {
416+
}
417+
412418
}

0 commit comments

Comments
 (0)