Skip to content

Commit 633d125

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 38b3099 commit 633d125

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;
@@ -41,6 +42,7 @@
4142
* @author Oliver Gierke
4243
* @author Christoph Strobl
4344
* @author Mark Paluch
45+
* @author Jens Schauder
4446
*/
4547
public abstract class AbstractPersistentProperty<P extends PersistentProperty<P>> implements PersistentProperty<P> {
4648

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

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

9395
this.isAssociation = Lazy.of(() -> ASSOCIATION_TYPE != null && ASSOCIATION_TYPE.isAssignableFrom(rawType));
@@ -317,7 +319,7 @@ public boolean equals(@Nullable Object obj) {
317319
return false;
318320
}
319321

320-
return this.property.equals(that.property);
322+
return this.property.equals(that.property) && this.owner.equals(that.owner);
321323
}
322324

323325
@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
@@ -81,16 +81,6 @@ void isEntityWorksForUntypedCollection() {
8181
assertThat(getProperty(TestClassComplex.class, "collection").isEntity()).isFalse();
8282
}
8383

84-
@Test // DATACMNS-121
85-
void considersPropertiesEqualIfFieldEquals() {
86-
87-
var firstProperty = getProperty(FirstConcrete.class, "genericField");
88-
var secondProperty = getProperty(SecondConcrete.class, "genericField");
89-
90-
assertThat(firstProperty).isEqualTo(secondProperty);
91-
assertThat(firstProperty.hashCode()).isEqualTo(secondProperty.hashCode());
92-
}
93-
9484
@Test // DATACMNS-180
9585
void doesNotConsiderJavaTransientFieldsTransient() {
9686
assertThat(getProperty(TestClassComplex.class, "transientField").isTransient()).isFalse();
@@ -207,7 +197,7 @@ void resolvesFieldNameWithUnderscoresCorrectly() {
207197
@Test // DATACMNS-1139
208198
void resolvesGenericsForRawType() {
209199

210-
var property = getProperty(FirstConcrete.class, "genericField");
200+
var property = getProperty(Concrete.class, "genericField");
211201

212202
assertThat(property.getRawType()).isEqualTo(String.class);
213203
}
@@ -240,6 +230,15 @@ void considersVavrMaps() {
240230
assertThat(property.isMap()).isTrue();
241231
}
242232

233+
@Test // GH-2972
234+
void equalsConsidersOwner() {
235+
236+
SamplePersistentProperty id1 = getProperty(Inherited1.class, "id");
237+
SamplePersistentProperty id2 = getProperty(Inherited2.class, "id");
238+
239+
assertThat(id1).isNotEqualTo(id2);
240+
}
241+
243242
private <T> BasicPersistentEntity<T, SamplePersistentProperty> getEntity(Class<T> type) {
244243
return new BasicPersistentEntity<>(TypeInformation.of(type));
245244
}
@@ -277,11 +276,7 @@ class Generic<T> {
277276

278277
}
279278

280-
class FirstConcrete extends Generic<String> {
281-
282-
}
283-
284-
class SecondConcrete extends Generic<Integer> {
279+
class Concrete extends Generic<String> {
285280

286281
}
287282

@@ -412,4 +407,15 @@ interface JMoleculesAggregate extends AggregateRoot<JMoleculesAggregate, Identif
412407
class VavrWrapper {
413408
io.vavr.collection.Map<String, String> vavrMap;
414409
}
410+
411+
class Base {
412+
Long id;
413+
}
414+
415+
class Inherited1 extends Base {
416+
}
417+
418+
class Inherited2 extends Base {
419+
}
420+
415421
}

0 commit comments

Comments
 (0)