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.
This commit is contained in:
@@ -316,6 +316,6 @@ public abstract class AbstractPersistentProperty<P extends PersistentProperty<P>
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("%s.%s : %s", getOwner().getType().getName(), getName(), getType().getName());
|
||||
return this.field.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -161,7 +161,10 @@ public class BasicPersistentEntity<T, P extends PersistentProperty<P>> implement
|
||||
|
||||
Assert.notNull(property);
|
||||
properties.add(property);
|
||||
propertyCache.put(property.getName(), property);
|
||||
|
||||
if (!propertyCache.containsKey(property.getName())) {
|
||||
propertyCache.put(property.getName(), property);
|
||||
}
|
||||
|
||||
if (property.isIdProperty()) {
|
||||
|
||||
|
||||
@@ -27,6 +27,7 @@ import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.mapping.PersistentEntity;
|
||||
import org.springframework.data.mapping.PropertyPath;
|
||||
import org.springframework.data.mapping.model.BasicPersistentEntity;
|
||||
@@ -142,6 +143,17 @@ public class AbstractMappingContextUnitTests {
|
||||
assertThat(entity.getPersistentProperty("metaClass"), is(nullValue()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-???
|
||||
*/
|
||||
@Test
|
||||
public void usesMostConcreteProperty() {
|
||||
|
||||
SampleMappingContext mappingContext = new SampleMappingContext();
|
||||
PersistentEntity<Object, SamplePersistentProperty> entity = mappingContext.getPersistentEntity(Extension.class);
|
||||
assertThat(entity.getPersistentProperty("foo").isIdProperty(), is(true));
|
||||
}
|
||||
|
||||
class Person {
|
||||
String name;
|
||||
}
|
||||
@@ -154,4 +166,13 @@ public class AbstractMappingContextUnitTests {
|
||||
|
||||
MetaClass metaClass;
|
||||
}
|
||||
|
||||
static class Base {
|
||||
String foo;
|
||||
}
|
||||
|
||||
static class Extension extends Base {
|
||||
@Id
|
||||
String foo;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user