Skip to content

Commit 3229330

Browse files
harawataIrvingOS
authored andcommitted
returnInstanceForEmptyRow should work for constructor auto-mapping
fixes mybatis#2665
1 parent 7c97952 commit 3229330

File tree

4 files changed

+60
-1
lines changed

4 files changed

+60
-1
lines changed

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,9 @@ private Object applyConstructorAutomapping(ResultSetWrapper rsw, ResultMap resul
750750
foundValues = applyColumnOrderBasedConstructorAutomapping(rsw, constructorArgTypes, constructorArgs, constructor,
751751
foundValues);
752752
}
753-
return foundValues ? objectFactory.create(resultType, constructorArgTypes, constructorArgs) : null;
753+
return foundValues || configuration.isReturnInstanceForEmptyRow()
754+
? objectFactory.create(resultType, constructorArgTypes, constructorArgs)
755+
: null;
754756
}
755757

756758
private boolean applyColumnOrderBasedConstructorAutomapping(ResultSetWrapper rsw, List<Class<?>> constructorArgTypes,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2009-2022 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+
* https://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.empty_row;
17+
18+
public class ImmutableParent {
19+
private final String col1;
20+
private final String col2;
21+
22+
public ImmutableParent(String col1, String col2) {
23+
super();
24+
this.col1 = col1;
25+
this.col2 = col2;
26+
}
27+
28+
public String getCol1() {
29+
return col1;
30+
}
31+
32+
public String getCol2() {
33+
return col2;
34+
}
35+
}

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

+3
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,7 @@ public interface Mapper {
5656
"left join child c on c.parent_id = p.id",
5757
"left join pet e on e.parent_id = p.id", "where p.id = #{id}" })
5858
Parent getTwoCollections(Integer id);
59+
60+
@Select("select col1, col2 from parent where id = #{id}")
61+
ImmutableParent selectImmutable(Integer id);
5962
}

src/test/java/org/apache/ibatis/submitted/empty_row/ReturnInstanceForEmptyRowTest.java

+19
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,23 @@ void shouldSquashMultipleEmptyResults() {
139139
assertNotNull(parent.getPets().get(0));
140140
}
141141
}
142+
143+
@Test
144+
void testConstructorAutomapping() {
145+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
146+
Mapper mapper = sqlSession.getMapper(Mapper.class);
147+
ImmutableParent parent = mapper.selectImmutable(1);
148+
assertNotNull(parent);
149+
}
150+
}
151+
152+
@Test
153+
void testArgNameBasedConstructorAutomapping() {
154+
sqlSessionFactory.getConfiguration().setArgNameBasedConstructorAutoMapping(true);
155+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
156+
Mapper mapper = sqlSession.getMapper(Mapper.class);
157+
ImmutableParent parent = mapper.selectImmutable(1);
158+
assertNotNull(parent);
159+
}
160+
}
142161
}

0 commit comments

Comments
 (0)