Skip to content

Commit

Permalink
Merge pull request #2804 from harawata/record-name-only-constructor
Browse files Browse the repository at this point in the history
Search readable property when resolving constructor arg type by name
  • Loading branch information
harawata authored Feb 10, 2023
2 parents 4c8153c + 43fcd30 commit 661e574
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ private Class<?> resolveResultJavaType(Class<?> resultType, String property, Cla
if (javaType == null && property != null) {
try {
MetaClass metaResultType = MetaClass.forClass(resultType, configuration.getReflectorFactory());
javaType = metaResultType.getSetterType(property);
javaType = metaResultType.getGetterType(property);
} catch (Exception e) {
// ignore, following null check statement will deal with the situation
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/apache/ibatis/mapping/ResultMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ public ResultMap build() {
final List<String> actualArgNames = argNamesOfMatchingConstructor(constructorArgNames);
if (actualArgNames == null) {
throw new BuilderException("Error in result map '" + resultMap.id + "'. Failed to find a constructor in '"
+ resultMap.getType().getName() + "' by arg names " + constructorArgNames
+ ". There might be more info in debug log.");
+ resultMap.getType().getName() + "' with arg names " + constructorArgNames
+ ". 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.");
}
resultMap.constructorResultMappings.sort((o1, o2) -> {
int paramIdx1 = actualArgNames.indexOf(o1.getProperty());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ public interface RecordTypeMapper {
@Select("select val, id, url from prop where id = #{id}")
Property selectProperty(int id);

@Arg(name = "id", column = "id", id = true)
@Arg(name = "value", column = "val")
@Arg(name = "URL", column = "url")
@Select("select val, id, url from prop where id = #{id}")
Property selectPropertyNoJavaType(int id);

@Insert("insert into prop (id, val, url) values (#{id}, #{value}, #{URL})")
int insertProperty(Property property);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ void testSelectRecord() {
}
}

@Test
void shouldResolveConstructorArgType() {
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
RecordTypeMapper mapper = sqlSession.getMapper(RecordTypeMapper.class);
Property prop = mapper.selectPropertyNoJavaType(1);
assertEquals("Val1!", prop.value());
assertEquals("https://www.google.com", prop.URL());
}
}

@Test
void testSelectRecordAutomapping() {
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
Expand Down

0 comments on commit 661e574

Please sign in to comment.