Skip to content

Commit 661e574

Browse files
authored
Merge pull request #2804 from harawata/record-name-only-constructor
Search readable property when resolving constructor arg type by name
2 parents 4c8153c + 43fcd30 commit 661e574

File tree

4 files changed

+19
-3
lines changed

4 files changed

+19
-3
lines changed

src/main/java/org/apache/ibatis/builder/MapperBuilderAssistant.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ private Class<?> resolveResultJavaType(Class<?> resultType, String property, Cla
437437
if (javaType == null && property != null) {
438438
try {
439439
MetaClass metaResultType = MetaClass.forClass(resultType, configuration.getReflectorFactory());
440-
javaType = metaResultType.getSetterType(property);
440+
javaType = metaResultType.getGetterType(property);
441441
} catch (Exception e) {
442442
// ignore, following null check statement will deal with the situation
443443
}

src/main/java/org/apache/ibatis/mapping/ResultMap.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ public ResultMap build() {
128128
final List<String> actualArgNames = argNamesOfMatchingConstructor(constructorArgNames);
129129
if (actualArgNames == null) {
130130
throw new BuilderException("Error in result map '" + resultMap.id + "'. Failed to find a constructor in '"
131-
+ resultMap.getType().getName() + "' by arg names " + constructorArgNames
132-
+ ". There might be more info in debug log.");
131+
+ resultMap.getType().getName() + "' with arg names " + constructorArgNames
132+
+ ". Note that 'javaType' is required when there is no readable property with the same name ('name' is optional, BTW). There might be more info in debug log.");
133133
}
134134
resultMap.constructorResultMappings.sort((o1, o2) -> {
135135
int paramIdx1 = actualArgNames.indexOf(o1.getProperty());

src/test/java/org/apache/ibatis/submitted/record_type/RecordTypeMapper.java

+6
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ public interface RecordTypeMapper {
3232
@Select("select val, id, url from prop where id = #{id}")
3333
Property selectProperty(int id);
3434

35+
@Arg(name = "id", column = "id", id = true)
36+
@Arg(name = "value", column = "val")
37+
@Arg(name = "URL", column = "url")
38+
@Select("select val, id, url from prop where id = #{id}")
39+
Property selectPropertyNoJavaType(int id);
40+
3541
@Insert("insert into prop (id, val, url) values (#{id}, #{value}, #{URL})")
3642
int insertProperty(Property property);
3743

src/test/java/org/apache/ibatis/submitted/record_type/RecordTypeTest.java

+10
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ void testSelectRecord() {
5252
}
5353
}
5454

55+
@Test
56+
void shouldResolveConstructorArgType() {
57+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
58+
RecordTypeMapper mapper = sqlSession.getMapper(RecordTypeMapper.class);
59+
Property prop = mapper.selectPropertyNoJavaType(1);
60+
assertEquals("Val1!", prop.value());
61+
assertEquals("https://www.google.com", prop.URL());
62+
}
63+
}
64+
5565
@Test
5666
void testSelectRecordAutomapping() {
5767
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {

0 commit comments

Comments
 (0)