Skip to content

Commit 58cc1a5

Browse files
committed
#93 auto-mapping should also be applied to deeply nested collection/association.
1 parent f19ff46 commit 58cc1a5

File tree

6 files changed

+69
-8
lines changed

6 files changed

+69
-8
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,7 @@ private Object getRowValue(ResultSetWrapper rsw, ResultMap resultMap, CacheKey c
748748
if (resultObject != null) {
749749
final MetaObject metaObject = configuration.newMetaObject(resultObject);
750750
ancestorObjects.put(absoluteKey, resultObject);
751-
applyNestedResultMappings(rsw, resultMap, metaObject, columnPrefix, combinedKey, false);
751+
applyNestedResultMappings(rsw, resultMap, metaObject, columnPrefix, combinedKey, false, parentAutoMapping);
752752
ancestorObjects.remove(absoluteKey);
753753
} else {
754754
final ResultLoaderMap lazyLoader = instantiateResultLoaderMap();
@@ -761,7 +761,7 @@ private Object getRowValue(ResultSetWrapper rsw, ResultMap resultMap, CacheKey c
761761
}
762762
foundValues = applyPropertyMappings(rsw, resultMap, metaObject, lazyLoader, columnPrefix) || foundValues;
763763
ancestorObjects.put(absoluteKey, resultObject);
764-
foundValues = applyNestedResultMappings(rsw, resultMap, metaObject, columnPrefix, combinedKey, true) || foundValues;
764+
foundValues = applyNestedResultMappings(rsw, resultMap, metaObject, columnPrefix, combinedKey, true, parentAutoMapping) || foundValues;
765765
ancestorObjects.remove(absoluteKey);
766766
foundValues = (lazyLoader != null && lazyLoader.size() > 0) || foundValues;
767767
resultObject = foundValues ? resultObject : null;
@@ -775,7 +775,7 @@ private Object getRowValue(ResultSetWrapper rsw, ResultMap resultMap, CacheKey c
775775
// NESTED RESULT MAP (JOIN MAPPING)
776776
//
777777

778-
private boolean applyNestedResultMappings(ResultSetWrapper rsw, ResultMap resultMap, MetaObject metaObject, String parentPrefix, CacheKey parentRowKey, boolean newObject) {
778+
private boolean applyNestedResultMappings(ResultSetWrapper rsw, ResultMap resultMap, MetaObject metaObject, String parentPrefix, CacheKey parentRowKey, boolean newObject, Boolean parentAutoMapping) {
779779
boolean foundValues = false;
780780
for (ResultMapping resultMapping : resultMap.getPropertyResultMappings()) {
781781
final String nestedResultMapId = resultMapping.getNestedResultMapId();
@@ -793,7 +793,7 @@ private boolean applyNestedResultMappings(ResultSetWrapper rsw, ResultMap result
793793
boolean knownValue = (rowValue != null);
794794
final Object collectionProperty = instantiateCollectionPropertyIfAppropriate(resultMapping, metaObject);
795795
if (anyNotNullColumnHasValue(resultMapping, columnPrefix, rsw.getResultSet())) {
796-
rowValue = getRowValue(rsw, nestedResultMap, combinedKey, rowKey, columnPrefix, rowValue, resultMap.getAutoMapping());
796+
rowValue = getRowValue(rsw, nestedResultMap, combinedKey, rowKey, columnPrefix, rowValue, parentAutoMapping != null ? parentAutoMapping : resultMap.getAutoMapping());
797797
if (rowValue != null && !knownValue) {
798798
if (collectionProperty != null) {
799799
final MetaObject targetMetaObject = configuration.newMetaObject(collectionProperty);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public void shouldGetAUserWithPets() {
7373
Assert.assertEquals("User2", user.getName());
7474
Assert.assertEquals(2, user.getPets().size());
7575
Assert.assertEquals(Integer.valueOf(12), user.getPets().get(0).getPetId());
76+
Assert.assertEquals("John", user.getPets().get(0).getBreeder().getBreederName());
7677
Assert.assertEquals("Kotetsu", user.getPets().get(1).getPetName());
7778
} finally {
7879
sqlSession.close();
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 Breeder {
19+
20+
private Integer breederId;
21+
private String breederName;
22+
23+
public Integer getBreederId() {
24+
return breederId;
25+
}
26+
27+
public void setBreederId(Integer breederId) {
28+
this.breederId = breederId;
29+
}
30+
31+
public String getBreederName() {
32+
return breederName;
33+
}
34+
35+
public void setBreederName(String breederName) {
36+
this.breederName = breederName;
37+
}
38+
}

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ create table books (
3131
create table pets (
3232
id int,
3333
owner int,
34+
breeder int,
35+
name varchar(20)
36+
);
37+
38+
create table breeder (
39+
id int,
3440
name varchar(20)
3541
);
3642

@@ -39,6 +45,8 @@ insert into users (id, name) values(2, 'User2');
3945

4046
insert into books (version, name) values(99, 'Learn Java');
4147

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');
48+
insert into pets (id, owner, breeder, name) values(11, 1, null, 'Ren');
49+
insert into pets (id, owner, breeder, name) values(12, 2, 101, 'Chien');
50+
insert into pets (id, owner, breeder, name) values(13, 2, null, 'Kotetsu');
51+
52+
insert into breeder (id, name) values(101, 'John');

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,20 @@
2828
</resultMap>
2929

3030
<select id="getUserWithPets" resultMap="resultWithPets">
31-
select users.id, users.name, pets.id as petId, pets.name as petName from users
31+
select users.id, users.name,
32+
pets.id as petId, pets.name as petName,
33+
breeder.id as breederId, breeder.name as breederName
34+
from users
3235
left join pets on pets.owner = users.id
36+
left join breeder on breeder.id = pets.breeder
3337
where users.id = #{id}
3438
</select>
3539

3640
<resultMap type="org.apache.ibatis.submitted.automapping.User" id="resultWithPets" autoMapping="true">
3741
<id column="id" property="id"/>
3842
<collection property="pets" ofType="org.apache.ibatis.submitted.automapping.Pet">
3943
<id column="petId" property="petId"/>
44+
<association property="breeder" javaType="org.apache.ibatis.submitted.automapping.Breeder" />
4045
</collection>
4146
</resultMap>
4247

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class Pet {
1919

2020
private Integer petId;
2121
private String petName;
22+
private Breeder breeder;
2223

2324
public Integer getPetId() {
2425
return petId;
@@ -35,4 +36,12 @@ public String getPetName() {
3536
public void setPetName(String petName) {
3637
this.petName = petName;
3738
}
39+
40+
public Breeder getBreeder() {
41+
return breeder;
42+
}
43+
44+
public void setBreeder(Breeder breeder) {
45+
this.breeder = breeder;
46+
}
3847
}

0 commit comments

Comments
 (0)