Skip to content

Commit

Permalink
fixes mybatis#93 When autoMapping is set to true for a resultMap, aut…
Browse files Browse the repository at this point in the history
…oMapping should be applied to the nested collection and/or association as well.
  • Loading branch information
harawata committed Oct 9, 2013
1 parent 7f7ca8c commit f19ff46
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ private Object getRowValue(ResultSetWrapper rsw, ResultMap resultMap, CacheKey r
if (resultObject != null && !typeHandlerRegistry.hasTypeHandler(resultMap.getType())) {
final MetaObject metaObject = configuration.newMetaObject(resultObject);
boolean foundValues = resultMap.getConstructorResultMappings().size() > 0;
if (shouldApplyAutomaticMappings(resultMap, !AutoMappingBehavior.NONE.equals(configuration.getAutoMappingBehavior()))) {
if (shouldApplyAutomaticMappings(resultMap, null, !AutoMappingBehavior.NONE.equals(configuration.getAutoMappingBehavior()))) {
foundValues = applyAutomaticMappings(rsw, resultMap, metaObject, null) || foundValues;
}
foundValues = applyPropertyMappings(rsw, resultMap, metaObject, lazyLoader, null) || foundValues;
Expand All @@ -341,8 +341,9 @@ private Object getRowValue(ResultSetWrapper rsw, ResultMap resultMap, CacheKey r
return resultObject;
}

private boolean shouldApplyAutomaticMappings(ResultMap resultMap, boolean def) {
return resultMap.getAutoMapping() != null ? resultMap.getAutoMapping() : def;
private boolean shouldApplyAutomaticMappings(ResultMap resultMap, Boolean parentAutoMapping, boolean def) {
return resultMap.getAutoMapping() != null ? resultMap.getAutoMapping()
: parentAutoMapping != null ? parentAutoMapping : def;
}

private ResultLoaderMap instantiateResultLoaderMap() {
Expand Down Expand Up @@ -725,9 +726,9 @@ private void handleRowValuesForNestedResultMap(ResultSetWrapper rsw, ResultMap r
nestedResultObjects.clear();
storeObject(resultHandler, resultContext, rowValue, parentMapping, rsw.getResultSet());
}
rowValue = getRowValue(rsw, discriminatedResultMap, rowKey, rowKey, null, partialObject);
rowValue = getRowValue(rsw, discriminatedResultMap, rowKey, rowKey, null, partialObject, null);
} else {
rowValue = getRowValue(rsw, discriminatedResultMap, rowKey, rowKey, null, partialObject);
rowValue = getRowValue(rsw, discriminatedResultMap, rowKey, rowKey, null, partialObject, null);
if (partialObject == null) {
storeObject(resultHandler, resultContext, rowValue, parentMapping, rsw.getResultSet());
}
Expand All @@ -742,7 +743,7 @@ private void handleRowValuesForNestedResultMap(ResultSetWrapper rsw, ResultMap r
// GET VALUE FROM ROW FOR NESTED RESULT MAP
//

private Object getRowValue(ResultSetWrapper rsw, ResultMap resultMap, CacheKey combinedKey, CacheKey absoluteKey, String columnPrefix, Object partialObject) throws SQLException {
private Object getRowValue(ResultSetWrapper rsw, ResultMap resultMap, CacheKey combinedKey, CacheKey absoluteKey, String columnPrefix, Object partialObject, Boolean parentAutoMapping) throws SQLException {
Object resultObject = partialObject;
if (resultObject != null) {
final MetaObject metaObject = configuration.newMetaObject(resultObject);
Expand All @@ -755,7 +756,7 @@ private Object getRowValue(ResultSetWrapper rsw, ResultMap resultMap, CacheKey c
if (resultObject != null && !typeHandlerRegistry.hasTypeHandler(resultMap.getType())) {
final MetaObject metaObject = configuration.newMetaObject(resultObject);
boolean foundValues = resultMap.getConstructorResultMappings().size() > 0;
if (shouldApplyAutomaticMappings(resultMap, AutoMappingBehavior.FULL.equals(configuration.getAutoMappingBehavior()))) {
if (shouldApplyAutomaticMappings(resultMap, parentAutoMapping, AutoMappingBehavior.FULL.equals(configuration.getAutoMappingBehavior()))) {
foundValues = applyAutomaticMappings(rsw, resultMap, metaObject, columnPrefix) || foundValues;
}
foundValues = applyPropertyMappings(rsw, resultMap, metaObject, lazyLoader, columnPrefix) || foundValues;
Expand Down Expand Up @@ -792,7 +793,7 @@ private boolean applyNestedResultMappings(ResultSetWrapper rsw, ResultMap result
boolean knownValue = (rowValue != null);
final Object collectionProperty = instantiateCollectionPropertyIfAppropriate(resultMapping, metaObject);
if (anyNotNullColumnHasValue(resultMapping, columnPrefix, rsw.getResultSet())) {
rowValue = getRowValue(rsw, nestedResultMap, combinedKey, rowKey, columnPrefix, rowValue);
rowValue = getRowValue(rsw, nestedResultMap, combinedKey, rowKey, columnPrefix, rowValue, resultMap.getAutoMapping());
if (rowValue != null && !knownValue) {
if (collectionProperty != null) {
final MetaObject targetMetaObject = configuration.newMetaObject(collectionProperty);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,22 @@ public void shouldGetAUser() {
}
}

@Test
public void shouldGetAUserWithPets() {
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
Mapper mapper = sqlSession.getMapper(Mapper.class);
User user = mapper.getUserWithPets(2);
Assert.assertEquals(Integer.valueOf(2), user.getId());
Assert.assertEquals("User2", user.getName());
Assert.assertEquals(2, user.getPets().size());
Assert.assertEquals(Integer.valueOf(12), user.getPets().get(0).getPetId());
Assert.assertEquals("Kotetsu", user.getPets().get(1).getPetName());
} finally {
sqlSession.close();
}
}

@Test
public void shouldGetBooks() {
//set automapping to default partial
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ create table books (
name varchar(20)
);

create table pets (
id int,
owner int,
name varchar(20)
);

insert into users (id, name) values(1, 'User1');
insert into users (id, name) values(2, 'User2');

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

insert into books (version, name) values(99, 'Learn Java');
insert into pets (id, owner, name) values(11, 1, 'Ren');
insert into pets (id, owner, name) values(12, 2, 'Chien');
insert into pets (id, owner, name) values(13, 2, 'Kotetsu');
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public interface Mapper {

User getUser(Integer id);

User getUserWithPets(Integer id);

List<Book> getBooks();

Article getArticle();
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/org/apache/ibatis/submitted/automapping/Mapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@

<resultMap type="org.apache.ibatis.submitted.automapping.User" id="result" autoMapping="true">
</resultMap>

<select id="getUserWithPets" resultMap="resultWithPets">
select users.id, users.name, pets.id as petId, pets.name as petName from users
left join pets on pets.owner = users.id
where users.id = #{id}
</select>

<resultMap type="org.apache.ibatis.submitted.automapping.User" id="resultWithPets" autoMapping="true">
<id column="id" property="id"/>
<collection property="pets" ofType="org.apache.ibatis.submitted.automapping.Pet">
<id column="petId" property="petId"/>
</collection>
</resultMap>

<resultMap type="org.apache.ibatis.submitted.automapping.Book" id="bookResult">
<result property="name" column="name"/>
Expand Down
38 changes: 38 additions & 0 deletions src/test/java/org/apache/ibatis/submitted/automapping/Pet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2009-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ibatis.submitted.automapping;

public class Pet {

private Integer petId;
private String petName;

public Integer getPetId() {
return petId;
}

public void setPetId(Integer petId) {
this.petId = petId;
}

public String getPetName() {
return petName;
}

public void setPetName(String petName) {
this.petName = petName;
}
}
11 changes: 11 additions & 0 deletions src/test/java/org/apache/ibatis/submitted/automapping/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@
*/
package org.apache.ibatis.submitted.automapping;

import java.util.List;

public class User {

private Integer id;
private String name;
private List<Pet> pets;

public Integer getId() {
return id;
Expand All @@ -35,4 +38,12 @@ public String getName() {
public void setName(String name) {
this.name = name;
}

public List<Pet> getPets() {
return pets;
}

public void setPets(List<Pet> pets) {
this.pets = pets;
}
}

0 comments on commit f19ff46

Please sign in to comment.