Skip to content

Commit 8bfd5a2

Browse files
committed
Fixes #103. Property mappings were ignored when handling constructors.
1 parent 20f8af8 commit 8bfd5a2

File tree

5 files changed

+59
-5
lines changed

5 files changed

+59
-5
lines changed

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ private void handleRowValuesForSimpleResultMap(ResultSetWrapper rsw, ResultMap r
286286
skipRows(rsw.getResultSet(), rowBounds);
287287
while (shouldProcessMoreRows(rsw.getResultSet(), resultContext, rowBounds)) {
288288
ResultMap discriminatedResultMap = resolveDiscriminatedResultMap(rsw.getResultSet(), resultMap, null);
289-
Object rowValue = getRowValue(rsw, discriminatedResultMap, null);
289+
Object rowValue = getRowValue(rsw, discriminatedResultMap);
290290
storeObject(resultHandler, resultContext, rowValue, parentMapping, rsw.getResultSet());
291291
}
292292
}
@@ -324,7 +324,7 @@ private void skipRows(ResultSet rs, RowBounds rowBounds) throws SQLException {
324324
// GET VALUE FROM ROW FOR SIMPLE RESULT MAP
325325
//
326326

327-
private Object getRowValue(ResultSetWrapper rsw, ResultMap resultMap, CacheKey rowKey) throws SQLException {
327+
private Object getRowValue(ResultSetWrapper rsw, ResultMap resultMap) throws SQLException {
328328
final ResultLoaderMap lazyLoader = instantiateResultLoaderMap();
329329
Object resultObject = createResultObject(rsw, resultMap, lazyLoader, null);
330330
if (resultObject != null && !typeHandlerRegistry.hasTypeHandler(resultMap.getType())) {
@@ -543,8 +543,7 @@ private Object createParameterizedResultObject(ResultSetWrapper rsw, Class<?> re
543543
value = getNestedQueryConstructorValue(rsw.getResultSet(), constructorMapping, columnPrefix);
544544
} else if (constructorMapping.getNestedResultMapId() != null) {
545545
final ResultMap resultMap = configuration.getResultMap(constructorMapping.getNestedResultMapId());
546-
final ResultLoaderMap lazyLoader = instantiateResultLoaderMap();
547-
value = createResultObject(rsw, resultMap, lazyLoader, columnPrefix);
546+
value = getRowValue(rsw, resultMap);
548547
} else {
549548
final TypeHandler<?> typeHandler = constructorMapping.getTypeHandler();
550549
value = typeHandler.getResult(rsw.getResultSet(), prependPrefix(column, columnPrefix));

src/test/java/org/apache/ibatis/binding/BindingTest.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,29 @@ public void shouldExecuteBoundSelectBlogUsingConstructorWithResultMap() {
376376
}
377377
}
378378

379+
@Test
380+
public void shouldExecuteBoundSelectBlogUsingConstructorWithResultMapAndProperties() {
381+
SqlSession session = sqlSessionFactory.openSession();
382+
try {
383+
BoundBlogMapper mapper = session.getMapper(BoundBlogMapper.class);
384+
Blog blog = mapper.selectBlogUsingConstructorWithResultMapAndProperties(1);
385+
assertEquals(1, blog.getId());
386+
assertEquals("Jim Business", blog.getTitle());
387+
assertNotNull("author should not be null", blog.getAuthor());
388+
Author author = blog.getAuthor();
389+
assertEquals(101, author.getId());
390+
assertEquals("[email protected]", author.getEmail());
391+
assertEquals("jim", author.getUsername());
392+
List<Post> posts = blog.getPosts();
393+
assertTrue("posts should not be empty", posts != null);
394+
assertEquals(2, posts.size());
395+
} finally {
396+
session.close();
397+
}
398+
}
399+
379400
@Ignore
380-
@Test // issue #480
401+
@Test // issue #480 and #101
381402
public void shouldExecuteBoundSelectBlogUsingConstructorWithResultMapCollection() {
382403
SqlSession session = sqlSessionFactory.openSession();
383404
try {

src/test/java/org/apache/ibatis/binding/BoundAuthorMapper.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@
3434
</constructor>
3535
</resultMap>
3636

37+
<resultMap id="authorResultMapWithProperties" type="domain.blog.Author">
38+
<id column="author_id" property="id"/>
39+
<result column="author_username" property="username" />
40+
<result column="author_password" property="password"/>
41+
<result column="author_email" property="email"/>
42+
<result column="author_bio" property="bio"/>
43+
<result column="favourite_section" javaType="domain.blog.Section"/>
44+
</resultMap>
45+
3746
<insert id="insertAuthor" parameterType="domain.blog.Author">
3847
<selectKey keyProperty="id" resultType="int" order="BEFORE">
3948
select CAST(RANDOM()*1000000 as INTEGER) a from SYSIBM.SYSDUMMY1

src/test/java/org/apache/ibatis/binding/BoundBlogMapper.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ public interface BoundBlogMapper {
111111

112112
Blog selectBlogUsingConstructorWithResultMap(int i);
113113

114+
Blog selectBlogUsingConstructorWithResultMapAndProperties(int i);
115+
114116
Blog selectBlogUsingConstructorWithResultMapCollection(int i);
115117

116118
Blog selectBlogByIdUsingConstructor(int id);

src/test/java/org/apache/ibatis/binding/BoundBlogMapper.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@
5858
</constructor>
5959
</resultMap>
6060

61+
<resultMap id="blogUsingConstructorWithResultMapAndProperties" type="Blog">
62+
<constructor>
63+
<idArg column="id" javaType="_int"/>
64+
<arg column="title" javaType="java.lang.String"/>
65+
<arg javaType="domain.blog.Author" resultMap="org.apache.ibatis.binding.BoundAuthorMapper.authorResultMapWithProperties"/>
66+
<arg column="id" javaType="java.util.List" select="selectPostsForBlog"/>
67+
</constructor>
68+
</resultMap>
69+
6170
<resultMap id="blogUsingConstructorWithResultMapCollection" type="Blog">
6271
<constructor>
6372
<idArg column="id" javaType="_int"/>
@@ -98,6 +107,20 @@
98107
on b.author_id = a.id
99108
where b.id = #{id}
100109
</select>
110+
111+
<select id="selectBlogUsingConstructorWithResultMapAndProperties" parameterType="int"
112+
resultMap="blogUsingConstructorWithResultMapAndProperties">
113+
select b.*,
114+
a.id as author_id,
115+
a.username as author_username,
116+
a.password as author_password,
117+
a.email as author_email,
118+
a.bio as author_bio,
119+
a.favourite_section
120+
from Blog b join Author a
121+
on b.author_id = a.id
122+
where b.id = #{id}
123+
</select>
101124

102125
<select id="selectBlogUsingConstructorWithResultMapCollection" parameterType="int"
103126
resultMap="blogUsingConstructorWithResultMapCollection">

0 commit comments

Comments
 (0)