diff --git a/src/main/java/org/apache/ibatis/executor/resultset/DefaultResultSetHandler.java b/src/main/java/org/apache/ibatis/executor/resultset/DefaultResultSetHandler.java index 0bbeb6d3ae9..22af84642e8 100644 --- a/src/main/java/org/apache/ibatis/executor/resultset/DefaultResultSetHandler.java +++ b/src/main/java/org/apache/ibatis/executor/resultset/DefaultResultSetHandler.java @@ -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> constructorArgTypes, diff --git a/src/test/java/org/apache/ibatis/submitted/empty_row/ImmutableParent.java b/src/test/java/org/apache/ibatis/submitted/empty_row/ImmutableParent.java new file mode 100644 index 00000000000..c6e8e9e9659 --- /dev/null +++ b/src/test/java/org/apache/ibatis/submitted/empty_row/ImmutableParent.java @@ -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; + } +} diff --git a/src/test/java/org/apache/ibatis/submitted/empty_row/Mapper.java b/src/test/java/org/apache/ibatis/submitted/empty_row/Mapper.java index f792693a151..e0c1d89e8c6 100644 --- a/src/test/java/org/apache/ibatis/submitted/empty_row/Mapper.java +++ b/src/test/java/org/apache/ibatis/submitted/empty_row/Mapper.java @@ -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); } diff --git a/src/test/java/org/apache/ibatis/submitted/empty_row/ReturnInstanceForEmptyRowTest.java b/src/test/java/org/apache/ibatis/submitted/empty_row/ReturnInstanceForEmptyRowTest.java index 049952a729d..46f67700c8c 100644 --- a/src/test/java/org/apache/ibatis/submitted/empty_row/ReturnInstanceForEmptyRowTest.java +++ b/src/test/java/org/apache/ibatis/submitted/empty_row/ReturnInstanceForEmptyRowTest.java @@ -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); + } + } }