DATAJDBC-252 - Optimize population of (immutable) entities to be created.
The entity creation not skips the property population entirely if the metamodel indicates that the instantiation already creates a complete entity. If there's need to actively populate properties, we now correctly skip the ones already consumed by the constructor. Original pull request: #86.
This commit is contained in:
committed by
Oliver Gierke
parent
63b9754180
commit
2a55aeb2b8
@@ -23,6 +23,7 @@ import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.data.mapping.MappingException;
|
||||
import org.springframework.data.mapping.PersistentProperty;
|
||||
import org.springframework.data.mapping.PersistentPropertyAccessor;
|
||||
import org.springframework.data.mapping.PreferredConstructor;
|
||||
import org.springframework.data.relational.core.conversion.RelationalConverter;
|
||||
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
|
||||
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
|
||||
@@ -69,12 +70,27 @@ public class EntityRowMapper<T> implements RowMapper<T> {
|
||||
|
||||
T result = createInstance(entity, resultSet, "");
|
||||
|
||||
if (entity.requiresPropertyPopulation()) {
|
||||
return populateProperties(result, resultSet);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private T populateProperties(T result, ResultSet resultSet) {
|
||||
|
||||
PersistentPropertyAccessor<T> propertyAccessor = converter.getPropertyAccessor(entity, result);
|
||||
|
||||
Object id = idProperty == null ? null : readFrom(resultSet, idProperty, "");
|
||||
|
||||
PreferredConstructor<T, RelationalPersistentProperty> persistenceConstructor = entity.getPersistenceConstructor();
|
||||
|
||||
for (RelationalPersistentProperty property : entity) {
|
||||
|
||||
if (persistenceConstructor != null && persistenceConstructor.isConstructorParameter(property)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (property.isCollectionLike() && id != null) {
|
||||
propertyAccessor.setProperty(property, accessStrategy.findAllByProperty(id, property));
|
||||
} else if (property.isMap() && id != null) {
|
||||
@@ -86,7 +102,7 @@ public class EntityRowMapper<T> implements RowMapper<T> {
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
return propertyAccessor.getBean();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user