@@ -100,6 +100,7 @@ public class DefaultResultSetHandler implements ResultSetHandler {
100
100
101
101
// Cached Automappings
102
102
private final Map <String , List <UnMappedColumnAutoMapping >> autoMappingsCache = new HashMap <>();
103
+ private final Map <String , List <String >> constructorAutoMappingColumns = new HashMap <>();
103
104
104
105
// temporary marking flag that indicate using constructor mapping (use field to reduce memory usage)
105
106
private boolean useConstructorMappings ;
@@ -524,6 +525,10 @@ private List<UnMappedColumnAutoMapping> createAutomaticMappings(ResultSetWrapper
524
525
if (autoMapping == null ) {
525
526
autoMapping = new ArrayList <>();
526
527
final List <String > unmappedColumnNames = rsw .getUnmappedColumnNames (resultMap , columnPrefix );
528
+ List <String > mappedInConstructorAutoMapping = constructorAutoMappingColumns .remove (mapKey );
529
+ if (mappedInConstructorAutoMapping != null ) {
530
+ unmappedColumnNames .removeAll (mappedInConstructorAutoMapping );
531
+ }
527
532
for (String columnName : unmappedColumnNames ) {
528
533
String propertyName = columnName ;
529
534
if (columnPrefix != null && !columnPrefix .isEmpty ()) {
@@ -660,7 +665,7 @@ private Object createResultObject(ResultSetWrapper rsw, ResultMap resultMap, Lis
660
665
} else if (resultType .isInterface () || metaType .hasDefaultConstructor ()) {
661
666
return objectFactory .create (resultType );
662
667
} else if (shouldApplyAutomaticMappings (resultMap , false )) {
663
- return createByConstructorSignature (rsw , resultType , constructorArgTypes , constructorArgs );
668
+ return createByConstructorSignature (rsw , resultMap , columnPrefix , resultType , constructorArgTypes , constructorArgs );
664
669
}
665
670
throw new ExecutorException ("Do not know how to create an instance of " + resultType );
666
671
}
@@ -692,9 +697,9 @@ Object createParameterizedResultObject(ResultSetWrapper rsw, Class<?> resultType
692
697
return foundValues ? objectFactory .create (resultType , constructorArgTypes , constructorArgs ) : null ;
693
698
}
694
699
695
- private Object createByConstructorSignature (ResultSetWrapper rsw , Class <?> resultType ,
700
+ private Object createByConstructorSignature (ResultSetWrapper rsw , ResultMap resultMap , String columnPrefix , Class <?> resultType ,
696
701
List <Class <?>> constructorArgTypes , List <Object > constructorArgs ) throws SQLException {
697
- return applyConstructorAutomapping (rsw , resultType , constructorArgTypes , constructorArgs ,
702
+ return applyConstructorAutomapping (rsw , resultMap , columnPrefix , resultType , constructorArgTypes , constructorArgs ,
698
703
findConstructorForAutomapping (resultType , rsw ).orElseThrow (() -> new ExecutorException (
699
704
"No constructor found in " + resultType .getName () + " matching " + rsw .getClassNames ())));
700
705
}
@@ -733,10 +738,10 @@ private boolean findUsableConstructorByArgTypes(final Constructor<?> constructor
733
738
return true ;
734
739
}
735
740
736
- private Object applyConstructorAutomapping (ResultSetWrapper rsw , Class <?> resultType , List <Class <?>> constructorArgTypes , List <Object > constructorArgs , Constructor <?> constructor ) throws SQLException {
741
+ private Object applyConstructorAutomapping (ResultSetWrapper rsw , ResultMap resultMap , String columnPrefix , Class <?> resultType , List <Class <?>> constructorArgTypes , List <Object > constructorArgs , Constructor <?> constructor ) throws SQLException {
737
742
boolean foundValues = false ;
738
743
if (configuration .isArgNameBasedConstructorAutoMapping ()) {
739
- foundValues = applyArgNameBasedConstructorAutoMapping (rsw , resultType , constructorArgTypes , constructorArgs ,
744
+ foundValues = applyArgNameBasedConstructorAutoMapping (rsw , resultMap , columnPrefix , resultType , constructorArgTypes , constructorArgs ,
740
745
constructor , foundValues );
741
746
} else {
742
747
foundValues = applyColumnOrderBasedConstructorAutomapping (rsw , constructorArgTypes , constructorArgs , constructor ,
@@ -759,7 +764,7 @@ private boolean applyColumnOrderBasedConstructorAutomapping(ResultSetWrapper rsw
759
764
return foundValues ;
760
765
}
761
766
762
- private boolean applyArgNameBasedConstructorAutoMapping (ResultSetWrapper rsw , Class <?> resultType ,
767
+ private boolean applyArgNameBasedConstructorAutoMapping (ResultSetWrapper rsw , ResultMap resultMap , String columnPrefix , Class <?> resultType ,
763
768
List <Class <?>> constructorArgTypes , List <Object > constructorArgs , Constructor <?> constructor , boolean foundValues )
764
769
throws SQLException {
765
770
List <String > missingArgs = null ;
@@ -776,6 +781,10 @@ private boolean applyArgNameBasedConstructorAutoMapping(ResultSetWrapper rsw, Cl
776
781
Object value = typeHandler .getResult (rsw .getResultSet (), columnName );
777
782
constructorArgTypes .add (paramType );
778
783
constructorArgs .add (value );
784
+ final String mapKey = resultMap .getId () + ":" + columnPrefix ;
785
+ if (!autoMappingsCache .containsKey (mapKey )) {
786
+ constructorAutoMappingColumns .computeIfAbsent (mapKey , k -> new ArrayList <>()).add (columnName );
787
+ }
779
788
columnNotFound = false ;
780
789
foundValues = value != null || foundValues ;
781
790
}
0 commit comments