Skip to content

Commit 24bc432

Browse files
committed
DATACMNS-332 - AbstractMappingContext now prefers most concrete property.
BasicPersistentEntity now only caches the most concrete property for a by-name lookup to mimic the behavior that was implemented in getPersistentProperty(String name). This is to prevent shadowed properties of superclasses leaking into the by-name lookups. Improved AbstractPersistentProperty.toString() to rather render the concrete field it is backed by.
1 parent 73b9d60 commit 24bc432

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,6 @@ public int hashCode() {
316316
*/
317317
@Override
318318
public String toString() {
319-
return String.format("%s.%s : %s", getOwner().getType().getName(), getName(), getType().getName());
319+
return this.field.toString();
320320
}
321321
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,10 @@ public void addPersistentProperty(P property) {
161161

162162
Assert.notNull(property);
163163
properties.add(property);
164-
propertyCache.put(property.getName(), property);
164+
165+
if (!propertyCache.containsKey(property.getName())) {
166+
propertyCache.put(property.getName(), property);
167+
}
165168

166169
if (property.isIdProperty()) {
167170

src/test/java/org/springframework/data/mapping/context/AbstractMappingContextUnitTests.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.mockito.Mockito;
2828
import org.springframework.context.ApplicationContext;
2929
import org.springframework.context.ApplicationEvent;
30+
import org.springframework.data.annotation.Id;
3031
import org.springframework.data.mapping.PersistentEntity;
3132
import org.springframework.data.mapping.PropertyPath;
3233
import org.springframework.data.mapping.model.BasicPersistentEntity;
@@ -142,6 +143,17 @@ public void doesNotCreatePersistentPropertyForGroovyMetaClass() {
142143
assertThat(entity.getPersistentProperty("metaClass"), is(nullValue()));
143144
}
144145

146+
/**
147+
* @see DATACMNS-???
148+
*/
149+
@Test
150+
public void usesMostConcreteProperty() {
151+
152+
SampleMappingContext mappingContext = new SampleMappingContext();
153+
PersistentEntity<Object, SamplePersistentProperty> entity = mappingContext.getPersistentEntity(Extension.class);
154+
assertThat(entity.getPersistentProperty("foo").isIdProperty(), is(true));
155+
}
156+
145157
class Person {
146158
String name;
147159
}
@@ -154,4 +166,13 @@ class Sample {
154166

155167
MetaClass metaClass;
156168
}
169+
170+
static class Base {
171+
String foo;
172+
}
173+
174+
static class Extension extends Base {
175+
@Id
176+
String foo;
177+
}
157178
}

0 commit comments

Comments
 (0)