43
43
import org .apache .ibatis .mapping .ResultMapping ;
44
44
import org .apache .ibatis .parsing .XNode ;
45
45
import org .apache .ibatis .parsing .XPathParser ;
46
+ import org .apache .ibatis .reflection .MetaClass ;
46
47
import org .apache .ibatis .session .Configuration ;
47
48
import org .apache .ibatis .type .JdbcType ;
48
49
import org .apache .ibatis .type .TypeHandler ;
@@ -249,10 +250,10 @@ private void resultMapElements(List<XNode> list) throws Exception {
249
250
}
250
251
251
252
private ResultMap resultMapElement (XNode resultMapNode ) throws Exception {
252
- return resultMapElement (resultMapNode , Collections .<ResultMapping > emptyList ());
253
+ return resultMapElement (resultMapNode , Collections .<ResultMapping > emptyList (), null );
253
254
}
254
255
255
- private ResultMap resultMapElement (XNode resultMapNode , List <ResultMapping > additionalResultMappings ) throws Exception {
256
+ private ResultMap resultMapElement (XNode resultMapNode , List <ResultMapping > additionalResultMappings , Class <?> enclosingType ) throws Exception {
256
257
ErrorContext .instance ().activity ("processing " + resultMapNode .getValueBasedIdentifier ());
257
258
String id = resultMapNode .getStringAttribute ("id" ,
258
259
resultMapNode .getValueBasedIdentifier ());
@@ -263,6 +264,9 @@ private ResultMap resultMapElement(XNode resultMapNode, List<ResultMapping> addi
263
264
String extend = resultMapNode .getStringAttribute ("extends" );
264
265
Boolean autoMapping = resultMapNode .getBooleanAttribute ("autoMapping" );
265
266
Class <?> typeClass = resolveClass (type );
267
+ if (typeClass == null ) {
268
+ typeClass = inheritEnclosingType (resultMapNode , enclosingType );
269
+ }
266
270
Discriminator discriminator = null ;
267
271
List <ResultMapping > resultMappings = new ArrayList <>();
268
272
resultMappings .addAll (additionalResultMappings );
@@ -289,6 +293,17 @@ private ResultMap resultMapElement(XNode resultMapNode, List<ResultMapping> addi
289
293
}
290
294
}
291
295
296
+ protected Class <?> inheritEnclosingType (XNode resultMapNode , Class <?> enclosingType ) {
297
+ if ("association" .equals (resultMapNode .getName ()) && resultMapNode .getStringAttribute ("resultMap" ) == null ) {
298
+ String property = resultMapNode .getStringAttribute ("property" );
299
+ if (property != null && enclosingType != null ) {
300
+ MetaClass metaResultType = MetaClass .forClass (enclosingType , configuration .getReflectorFactory ());
301
+ return metaResultType .getSetterType (property );
302
+ }
303
+ }
304
+ return null ;
305
+ }
306
+
292
307
private void processConstructorElement (XNode resultChild , Class <?> resultType , List <ResultMapping > resultMappings ) throws Exception {
293
308
List <XNode > argChildren = resultChild .getChildren ();
294
309
for (XNode argChild : argChildren ) {
@@ -313,7 +328,7 @@ private Discriminator processDiscriminatorElement(XNode context, Class<?> result
313
328
Map <String , String > discriminatorMap = new HashMap <>();
314
329
for (XNode caseChild : context .getChildren ()) {
315
330
String value = caseChild .getStringAttribute ("value" );
316
- String resultMap = caseChild .getStringAttribute ("resultMap" , processNestedResultMappings (caseChild , resultMappings ));
331
+ String resultMap = caseChild .getStringAttribute ("resultMap" , processNestedResultMappings (caseChild , resultMappings , resultType ));
317
332
discriminatorMap .put (value , resultMap );
318
333
}
319
334
return builderAssistant .buildDiscriminator (resultType , column , javaTypeClass , jdbcTypeEnum , typeHandlerClass , discriminatorMap );
@@ -369,7 +384,7 @@ private ResultMapping buildResultMappingFromContext(XNode context, Class<?> resu
369
384
String jdbcType = context .getStringAttribute ("jdbcType" );
370
385
String nestedSelect = context .getStringAttribute ("select" );
371
386
String nestedResultMap = context .getStringAttribute ("resultMap" ,
372
- processNestedResultMappings (context , Collections .<ResultMapping > emptyList ()));
387
+ processNestedResultMappings (context , Collections .<ResultMapping > emptyList (), resultType ));
373
388
String notNullColumn = context .getStringAttribute ("notNullColumn" );
374
389
String columnPrefix = context .getStringAttribute ("columnPrefix" );
375
390
String typeHandler = context .getStringAttribute ("typeHandler" );
@@ -382,13 +397,13 @@ private ResultMapping buildResultMappingFromContext(XNode context, Class<?> resu
382
397
JdbcType jdbcTypeEnum = resolveJdbcType (jdbcType );
383
398
return builderAssistant .buildResultMapping (resultType , property , column , javaTypeClass , jdbcTypeEnum , nestedSelect , nestedResultMap , notNullColumn , columnPrefix , typeHandlerClass , flags , resultSet , foreignColumn , lazy );
384
399
}
385
-
386
- private String processNestedResultMappings (XNode context , List <ResultMapping > resultMappings ) throws Exception {
400
+
401
+ private String processNestedResultMappings (XNode context , List <ResultMapping > resultMappings , Class <?> enclosingType ) throws Exception {
387
402
if ("association" .equals (context .getName ())
388
403
|| "collection" .equals (context .getName ())
389
404
|| "case" .equals (context .getName ())) {
390
405
if (context .getStringAttribute ("select" ) == null ) {
391
- ResultMap resultMap = resultMapElement (context , resultMappings );
406
+ ResultMap resultMap = resultMapElement (context , resultMappings , enclosingType );
392
407
return resultMap .getId ();
393
408
}
394
409
}
0 commit comments