Skip to content

Commit f19ff46

Browse files
committed
fixes #93 When autoMapping is set to true for a resultMap, autoMapping should be applied to the nested collection and/or association as well.
1 parent 7f7ca8c commit f19ff46

File tree

7 files changed

+101
-9
lines changed

7 files changed

+101
-9
lines changed

src/main/java/org/apache/ibatis/executor/resultset/DefaultResultSetHandler.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ private Object getRowValue(ResultSetWrapper rsw, ResultMap resultMap, CacheKey r
330330
if (resultObject != null && !typeHandlerRegistry.hasTypeHandler(resultMap.getType())) {
331331
final MetaObject metaObject = configuration.newMetaObject(resultObject);
332332
boolean foundValues = resultMap.getConstructorResultMappings().size() > 0;
333-
if (shouldApplyAutomaticMappings(resultMap, !AutoMappingBehavior.NONE.equals(configuration.getAutoMappingBehavior()))) {
333+
if (shouldApplyAutomaticMappings(resultMap, null, !AutoMappingBehavior.NONE.equals(configuration.getAutoMappingBehavior()))) {
334334
foundValues = applyAutomaticMappings(rsw, resultMap, metaObject, null) || foundValues;
335335
}
336336
foundValues = applyPropertyMappings(rsw, resultMap, metaObject, lazyLoader, null) || foundValues;
@@ -341,8 +341,9 @@ private Object getRowValue(ResultSetWrapper rsw, ResultMap resultMap, CacheKey r
341341
return resultObject;
342342
}
343343

344-
private boolean shouldApplyAutomaticMappings(ResultMap resultMap, boolean def) {
345-
return resultMap.getAutoMapping() != null ? resultMap.getAutoMapping() : def;
344+
private boolean shouldApplyAutomaticMappings(ResultMap resultMap, Boolean parentAutoMapping, boolean def) {
345+
return resultMap.getAutoMapping() != null ? resultMap.getAutoMapping()
346+
: parentAutoMapping != null ? parentAutoMapping : def;
346347
}
347348

348349
private ResultLoaderMap instantiateResultLoaderMap() {
@@ -725,9 +726,9 @@ private void handleRowValuesForNestedResultMap(ResultSetWrapper rsw, ResultMap r
725726
nestedResultObjects.clear();
726727
storeObject(resultHandler, resultContext, rowValue, parentMapping, rsw.getResultSet());
727728
}
728-
rowValue = getRowValue(rsw, discriminatedResultMap, rowKey, rowKey, null, partialObject);
729+
rowValue = getRowValue(rsw, discriminatedResultMap, rowKey, rowKey, null, partialObject, null);
729730
} else {
730-
rowValue = getRowValue(rsw, discriminatedResultMap, rowKey, rowKey, null, partialObject);
731+
rowValue = getRowValue(rsw, discriminatedResultMap, rowKey, rowKey, null, partialObject, null);
731732
if (partialObject == null) {
732733
storeObject(resultHandler, resultContext, rowValue, parentMapping, rsw.getResultSet());
733734
}
@@ -742,7 +743,7 @@ private void handleRowValuesForNestedResultMap(ResultSetWrapper rsw, ResultMap r
742743
// GET VALUE FROM ROW FOR NESTED RESULT MAP
743744
//
744745

745-
private Object getRowValue(ResultSetWrapper rsw, ResultMap resultMap, CacheKey combinedKey, CacheKey absoluteKey, String columnPrefix, Object partialObject) throws SQLException {
746+
private Object getRowValue(ResultSetWrapper rsw, ResultMap resultMap, CacheKey combinedKey, CacheKey absoluteKey, String columnPrefix, Object partialObject, Boolean parentAutoMapping) throws SQLException {
746747
Object resultObject = partialObject;
747748
if (resultObject != null) {
748749
final MetaObject metaObject = configuration.newMetaObject(resultObject);
@@ -755,7 +756,7 @@ private Object getRowValue(ResultSetWrapper rsw, ResultMap resultMap, CacheKey c
755756
if (resultObject != null && !typeHandlerRegistry.hasTypeHandler(resultMap.getType())) {
756757
final MetaObject metaObject = configuration.newMetaObject(resultObject);
757758
boolean foundValues = resultMap.getConstructorResultMappings().size() > 0;
758-
if (shouldApplyAutomaticMappings(resultMap, AutoMappingBehavior.FULL.equals(configuration.getAutoMappingBehavior()))) {
759+
if (shouldApplyAutomaticMappings(resultMap, parentAutoMapping, AutoMappingBehavior.FULL.equals(configuration.getAutoMappingBehavior()))) {
759760
foundValues = applyAutomaticMappings(rsw, resultMap, metaObject, columnPrefix) || foundValues;
760761
}
761762
foundValues = applyPropertyMappings(rsw, resultMap, metaObject, lazyLoader, columnPrefix) || foundValues;
@@ -792,7 +793,7 @@ private boolean applyNestedResultMappings(ResultSetWrapper rsw, ResultMap result
792793
boolean knownValue = (rowValue != null);
793794
final Object collectionProperty = instantiateCollectionPropertyIfAppropriate(resultMapping, metaObject);
794795
if (anyNotNullColumnHasValue(resultMapping, columnPrefix, rsw.getResultSet())) {
795-
rowValue = getRowValue(rsw, nestedResultMap, combinedKey, rowKey, columnPrefix, rowValue);
796+
rowValue = getRowValue(rsw, nestedResultMap, combinedKey, rowKey, columnPrefix, rowValue, resultMap.getAutoMapping());
796797
if (rowValue != null && !knownValue) {
797798
if (collectionProperty != null) {
798799
final MetaObject targetMetaObject = configuration.newMetaObject(collectionProperty);

src/test/java/org/apache/ibatis/submitted/automapping/AutomappingTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,22 @@ public void shouldGetAUser() {
6363
}
6464
}
6565

66+
@Test
67+
public void shouldGetAUserWithPets() {
68+
SqlSession sqlSession = sqlSessionFactory.openSession();
69+
try {
70+
Mapper mapper = sqlSession.getMapper(Mapper.class);
71+
User user = mapper.getUserWithPets(2);
72+
Assert.assertEquals(Integer.valueOf(2), user.getId());
73+
Assert.assertEquals("User2", user.getName());
74+
Assert.assertEquals(2, user.getPets().size());
75+
Assert.assertEquals(Integer.valueOf(12), user.getPets().get(0).getPetId());
76+
Assert.assertEquals("Kotetsu", user.getPets().get(1).getPetName());
77+
} finally {
78+
sqlSession.close();
79+
}
80+
}
81+
6682
@Test
6783
public void shouldGetBooks() {
6884
//set automapping to default partial

src/test/java/org/apache/ibatis/submitted/automapping/CreateDB.sql

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@ create table books (
2828
name varchar(20)
2929
);
3030

31+
create table pets (
32+
id int,
33+
owner int,
34+
name varchar(20)
35+
);
36+
3137
insert into users (id, name) values(1, 'User1');
38+
insert into users (id, name) values(2, 'User2');
39+
40+
insert into books (version, name) values(99, 'Learn Java');
3241

33-
insert into books (version, name) values(99, 'Learn Java');
42+
insert into pets (id, owner, name) values(11, 1, 'Ren');
43+
insert into pets (id, owner, name) values(12, 2, 'Chien');
44+
insert into pets (id, owner, name) values(13, 2, 'Kotetsu');

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public interface Mapper {
2121

2222
User getUser(Integer id);
2323

24+
User getUserWithPets(Integer id);
25+
2426
List<Book> getBooks();
2527

2628
Article getArticle();

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,19 @@
2626

2727
<resultMap type="org.apache.ibatis.submitted.automapping.User" id="result" autoMapping="true">
2828
</resultMap>
29+
30+
<select id="getUserWithPets" resultMap="resultWithPets">
31+
select users.id, users.name, pets.id as petId, pets.name as petName from users
32+
left join pets on pets.owner = users.id
33+
where users.id = #{id}
34+
</select>
35+
36+
<resultMap type="org.apache.ibatis.submitted.automapping.User" id="resultWithPets" autoMapping="true">
37+
<id column="id" property="id"/>
38+
<collection property="pets" ofType="org.apache.ibatis.submitted.automapping.Pet">
39+
<id column="petId" property="petId"/>
40+
</collection>
41+
</resultMap>
2942

3043
<resultMap type="org.apache.ibatis.submitted.automapping.Book" id="bookResult">
3144
<result property="name" column="name"/>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2009-2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.ibatis.submitted.automapping;
17+
18+
public class Pet {
19+
20+
private Integer petId;
21+
private String petName;
22+
23+
public Integer getPetId() {
24+
return petId;
25+
}
26+
27+
public void setPetId(Integer petId) {
28+
this.petId = petId;
29+
}
30+
31+
public String getPetName() {
32+
return petName;
33+
}
34+
35+
public void setPetName(String petName) {
36+
this.petName = petName;
37+
}
38+
}

src/test/java/org/apache/ibatis/submitted/automapping/User.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@
1515
*/
1616
package org.apache.ibatis.submitted.automapping;
1717

18+
import java.util.List;
19+
1820
public class User {
1921

2022
private Integer id;
2123
private String name;
24+
private List<Pet> pets;
2225

2326
public Integer getId() {
2427
return id;
@@ -35,4 +38,12 @@ public String getName() {
3538
public void setName(String name) {
3639
this.name = name;
3740
}
41+
42+
public List<Pet> getPets() {
43+
return pets;
44+
}
45+
46+
public void setPets(List<Pet> pets) {
47+
this.pets = pets;
48+
}
3849
}

0 commit comments

Comments
 (0)