Skip to content

Commit

Permalink
returnInstanceForEmptyRow should work for constructor auto-mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
harawata authored and IrvingOS committed Sep 4, 2022
1 parent a96688b commit 9309407
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,9 @@ private Object applyConstructorAutomapping(ResultSetWrapper rsw, ResultMap resul
foundValues = applyColumnOrderBasedConstructorAutomapping(rsw, constructorArgTypes, constructorArgs, constructor,
foundValues);
}
return foundValues ? objectFactory.create(resultType, constructorArgTypes, constructorArgs) : null;
return foundValues || configuration.isReturnInstanceForEmptyRow()
? objectFactory.create(resultType, constructorArgTypes, constructorArgs)
: null;
}

private boolean applyColumnOrderBasedConstructorAutomapping(ResultSetWrapper rsw, List<Class<?>> constructorArgTypes,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2009-2022 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
*
* https://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.empty_row;

public class ImmutableParent {
private final String col1;
private final String col2;

public ImmutableParent(String col1, String col2) {
super();
this.col1 = col1;
this.col2 = col2;
}

public String getCol1() {
return col1;
}

public String getCol2() {
return col2;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,7 @@ public interface Mapper {
"left join child c on c.parent_id = p.id",
"left join pet e on e.parent_id = p.id", "where p.id = #{id}" })
Parent getTwoCollections(Integer id);

@Select("select col1, col2 from parent where id = #{id}")
ImmutableParent selectImmutable(Integer id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,23 @@ void shouldSquashMultipleEmptyResults() {
assertNotNull(parent.getPets().get(0));
}
}

@Test
void testConstructorAutomapping() {
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
Mapper mapper = sqlSession.getMapper(Mapper.class);
ImmutableParent parent = mapper.selectImmutable(1);
assertNotNull(parent);
}
}

@Test
void testArgNameBasedConstructorAutomapping() {
sqlSessionFactory.getConfiguration().setArgNameBasedConstructorAutoMapping(true);
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
Mapper mapper = sqlSession.getMapper(Mapper.class);
ImmutableParent parent = mapper.selectImmutable(1);
assertNotNull(parent);
}
}
}

0 comments on commit 9309407

Please sign in to comment.