Skip to content

Commit c797058

Browse files
committed
fixes #1381 When there is no 'resultType' nor 'resultMap' specified for <association />, use the property's java type.
1 parent 5e1bcba commit c797058

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

src/main/java/org/apache/ibatis/builder/xml/XMLMapperBuilder.java

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.apache.ibatis.mapping.ResultMapping;
4444
import org.apache.ibatis.parsing.XNode;
4545
import org.apache.ibatis.parsing.XPathParser;
46+
import org.apache.ibatis.reflection.MetaClass;
4647
import org.apache.ibatis.session.Configuration;
4748
import org.apache.ibatis.type.JdbcType;
4849
import org.apache.ibatis.type.TypeHandler;
@@ -249,10 +250,10 @@ private void resultMapElements(List<XNode> list) throws Exception {
249250
}
250251

251252
private ResultMap resultMapElement(XNode resultMapNode) throws Exception {
252-
return resultMapElement(resultMapNode, Collections.<ResultMapping> emptyList());
253+
return resultMapElement(resultMapNode, Collections.<ResultMapping> emptyList(), null);
253254
}
254255

255-
private ResultMap resultMapElement(XNode resultMapNode, List<ResultMapping> additionalResultMappings) throws Exception {
256+
private ResultMap resultMapElement(XNode resultMapNode, List<ResultMapping> additionalResultMappings, Class<?> enclosingType) throws Exception {
256257
ErrorContext.instance().activity("processing " + resultMapNode.getValueBasedIdentifier());
257258
String id = resultMapNode.getStringAttribute("id",
258259
resultMapNode.getValueBasedIdentifier());
@@ -263,6 +264,9 @@ private ResultMap resultMapElement(XNode resultMapNode, List<ResultMapping> addi
263264
String extend = resultMapNode.getStringAttribute("extends");
264265
Boolean autoMapping = resultMapNode.getBooleanAttribute("autoMapping");
265266
Class<?> typeClass = resolveClass(type);
267+
if (typeClass == null) {
268+
typeClass = inheritEnclosingType(resultMapNode, enclosingType);
269+
}
266270
Discriminator discriminator = null;
267271
List<ResultMapping> resultMappings = new ArrayList<>();
268272
resultMappings.addAll(additionalResultMappings);
@@ -289,6 +293,17 @@ private ResultMap resultMapElement(XNode resultMapNode, List<ResultMapping> addi
289293
}
290294
}
291295

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+
292307
private void processConstructorElement(XNode resultChild, Class<?> resultType, List<ResultMapping> resultMappings) throws Exception {
293308
List<XNode> argChildren = resultChild.getChildren();
294309
for (XNode argChild : argChildren) {
@@ -313,7 +328,7 @@ private Discriminator processDiscriminatorElement(XNode context, Class<?> result
313328
Map<String, String> discriminatorMap = new HashMap<>();
314329
for (XNode caseChild : context.getChildren()) {
315330
String value = caseChild.getStringAttribute("value");
316-
String resultMap = caseChild.getStringAttribute("resultMap", processNestedResultMappings(caseChild, resultMappings));
331+
String resultMap = caseChild.getStringAttribute("resultMap", processNestedResultMappings(caseChild, resultMappings, resultType));
317332
discriminatorMap.put(value, resultMap);
318333
}
319334
return builderAssistant.buildDiscriminator(resultType, column, javaTypeClass, jdbcTypeEnum, typeHandlerClass, discriminatorMap);
@@ -369,7 +384,7 @@ private ResultMapping buildResultMappingFromContext(XNode context, Class<?> resu
369384
String jdbcType = context.getStringAttribute("jdbcType");
370385
String nestedSelect = context.getStringAttribute("select");
371386
String nestedResultMap = context.getStringAttribute("resultMap",
372-
processNestedResultMappings(context, Collections.<ResultMapping> emptyList()));
387+
processNestedResultMappings(context, Collections.<ResultMapping> emptyList(), resultType));
373388
String notNullColumn = context.getStringAttribute("notNullColumn");
374389
String columnPrefix = context.getStringAttribute("columnPrefix");
375390
String typeHandler = context.getStringAttribute("typeHandler");
@@ -382,13 +397,13 @@ private ResultMapping buildResultMappingFromContext(XNode context, Class<?> resu
382397
JdbcType jdbcTypeEnum = resolveJdbcType(jdbcType);
383398
return builderAssistant.buildResultMapping(resultType, property, column, javaTypeClass, jdbcTypeEnum, nestedSelect, nestedResultMap, notNullColumn, columnPrefix, typeHandlerClass, flags, resultSet, foreignColumn, lazy);
384399
}
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 {
387402
if ("association".equals(context.getName())
388403
|| "collection".equals(context.getName())
389404
|| "case".equals(context.getName())) {
390405
if (context.getStringAttribute("select") == null) {
391-
ResultMap resultMap = resultMapElement(context, resultMappings);
406+
ResultMap resultMap = resultMapElement(context, resultMappings, enclosingType);
392407
return resultMap.getId();
393408
}
394409
}

src/test/java/org/apache/ibatis/submitted/associationtest/Mapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2015 the original author or authors.
2+
* Copyright 2009-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

src/test/java/org/apache/ibatis/submitted/associationtest/Mapper.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!--
33
4-
Copyright 2009-2016 the original author or authors.
4+
Copyright 2009-2018 the original author or authors.
55
66
Licensed under the Apache License, Version 2.0 (the "License");
77
you may not use this file except in compliance with the License.

0 commit comments

Comments
 (0)