From 9f4ec7b23856a82fa7f95cfc240fe221bd7dff45 Mon Sep 17 00:00:00 2001 From: mhyeon-lee Date: Fri, 15 May 2020 18:42:27 +0900 Subject: [PATCH] DATAJDBC-546 improve performance for populate properties --- .../jdbc/core/convert/BasicJdbcConverter.java | 34 ++++++++++--------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/BasicJdbcConverter.java b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/BasicJdbcConverter.java index e4d06f0dd1..4a0674abba 100644 --- a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/BasicJdbcConverter.java +++ b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/BasicJdbcConverter.java @@ -19,8 +19,8 @@ import java.sql.JDBCType; import java.sql.ResultSet; import java.sql.SQLException; -import java.util.Map; -import java.util.Optional; +import java.util.*; +import java.util.function.Predicate; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -31,7 +31,6 @@ import org.springframework.data.jdbc.support.JdbcUtil; import org.springframework.data.mapping.PersistentPropertyAccessor; import org.springframework.data.mapping.PersistentPropertyPath; -import org.springframework.data.mapping.PreferredConstructor; import org.springframework.data.mapping.context.MappingContext; import org.springframework.data.mapping.model.SimpleTypeHolder; import org.springframework.data.relational.core.conversion.BasicRelationalConverter; @@ -387,26 +386,16 @@ T mapRow() { return createInstanceInternal(idValue); } - private T populateProperties(T instance, @Nullable Object idValue) { + private T populateProperties(T instance, @Nullable Object idValue, Predicate skipProperty) { PersistentPropertyAccessor propertyAccessor = getPropertyAccessor(entity, instance); - PreferredConstructor persistenceConstructor = entity.getPersistenceConstructor(); - for (RelationalPersistentProperty property : entity) { - if (persistenceConstructor != null && persistenceConstructor.isConstructorParameter(property)) { + if (skipProperty.test(property)) { continue; } - // skip absent simple properties - if (isSimpleProperty(property)) { - - if (!propertyValueProvider.hasProperty(property)) { - continue; - } - } - Object value = readOrLoadProperty(idValue, property); propertyAccessor.setProperty(property, value); } @@ -525,6 +514,8 @@ private Object readEntityFrom(RelationalPersistentProperty property) { private T createInstanceInternal(@Nullable Object idValue) { + Set constructorProperties = new HashSet<>(); + T instance = createInstance(entity, parameter -> { String parameterName = parameter.getName(); @@ -532,9 +523,20 @@ private T createInstanceInternal(@Nullable Object idValue) { Assert.notNull(parameterName, "A constructor parameter name must not be null to be used with Spring Data JDBC"); RelationalPersistentProperty property = entity.getRequiredPersistentProperty(parameterName); + constructorProperties.add(property); + return readOrLoadProperty(idValue, property); }); - return populateProperties(instance, idValue); + + return populateProperties(instance, idValue, property -> { + + if (constructorProperties.contains(property)) { + return true; + } + + // skip absent simple properties + return isSimpleProperty(property) && !propertyValueProvider.hasProperty(property); + }); } }