Skip to content

Commit 7a1bfad

Browse files
committed
Added tests that maps nested cursor using type handler
This probably worked in earlier versions, so these tests are to ensure backward compatibility. #566 (comment)
1 parent f19408b commit 7a1bfad

File tree

5 files changed

+107
-1
lines changed

5 files changed

+107
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public ResultMap build() {
117117
// #101
118118
Class<?> javaType = resultMapping.getJavaType();
119119
resultMap.hasResultMapsUsingConstructorCollection = resultMap.hasResultMapsUsingConstructorCollection
120-
|| (resultMapping.getNestedQueryId() == null && javaType != null
120+
|| (resultMapping.getNestedQueryId() == null && resultMapping.getTypeHandler() == null && javaType != null
121121
&& resultMap.configuration.getObjectFactory().isCollection(javaType));
122122

123123
if (resultMapping.getProperty() != null) {
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2009-2025 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+
17+
package org.apache.ibatis.submitted.oracle_cursor;
18+
19+
import java.sql.CallableStatement;
20+
import java.sql.PreparedStatement;
21+
import java.sql.ResultSet;
22+
import java.sql.SQLException;
23+
import java.util.ArrayList;
24+
import java.util.List;
25+
26+
import org.apache.ibatis.type.BaseTypeHandler;
27+
import org.apache.ibatis.type.JdbcType;
28+
29+
public class BooksTypeHandler extends BaseTypeHandler<List<Book>> {
30+
31+
@Override
32+
public void setNonNullParameter(PreparedStatement ps, int i, List<Book> parameter, JdbcType jdbcType)
33+
throws SQLException {
34+
// n/a
35+
}
36+
37+
@Override
38+
public List<Book> getNullableResult(ResultSet rs, String columnName) throws SQLException {
39+
List<Book> list = new ArrayList<>();
40+
try (ResultSet nestedCursor = rs.getObject(columnName, ResultSet.class)) {
41+
while (nestedCursor.next()) {
42+
Integer id = nestedCursor.getInt("id");
43+
String name = nestedCursor.getString("name");
44+
list.add(new Book(id, name));
45+
}
46+
}
47+
return list;
48+
}
49+
50+
@Override
51+
public List<Book> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
52+
// n/a
53+
return null;
54+
}
55+
56+
@Override
57+
public List<Book> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
58+
// n/a
59+
return null;
60+
}
61+
62+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ public interface Mapper {
3535

3636
List<Author> selectNestedCursorConstructorCollection();
3737

38+
List<Author> selectNestedCursorTypeHandler();
39+
40+
List<Author> selectNestedCursorTypeHandlerConstructor();
41+
3842
List<Author2> selectNestedCursorOfStrings();
3943

4044
List<Book2> selectNestedCursorAssociation();

src/test/java/org/apache/ibatis/submitted/oracle_cursor/OracleCursorTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,16 @@ void nestedCursorsConstructorCollection() {
9393
doTest(Mapper::selectNestedCursorConstructorCollection);
9494
}
9595

96+
@Test
97+
void nestedCursorsTypeHandler() {
98+
doTest(Mapper::selectNestedCursorTypeHandler);
99+
}
100+
101+
@Test
102+
void nestedCursorsTypeHandlerConstructor() {
103+
doTest(Mapper::selectNestedCursorTypeHandlerConstructor);
104+
}
105+
96106
private void doTest(Function<Mapper, List<Author>> query) {
97107
doTest(query, ArrayList.class);
98108
}

src/test/resources/org/apache/ibatis/submitted/oracle_cursor/Mapper.xml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,36 @@
128128
<include refid="nestedCursor" />
129129
</select>
130130

131+
<!-- Map nested cursor to property using type handler. -->
132+
<resultMap id="authorRM_TypeHandler"
133+
type="org.apache.ibatis.submitted.oracle_cursor.Author">
134+
<id property="id" column="id" />
135+
<result property="name" column="name" />
136+
<result property="books" column="books"
137+
typeHandler="org.apache.ibatis.submitted.oracle_cursor.BooksTypeHandler" />
138+
</resultMap>
139+
140+
<select id="selectNestedCursorTypeHandler"
141+
resultMap="authorRM_TypeHandler">
142+
<include refid="nestedCursor" />
143+
</select>
144+
145+
<!-- Map nested cursor to constructor arg using type handler. -->
146+
<resultMap id="authorRM_TypeHandlerConstructor"
147+
type="org.apache.ibatis.submitted.oracle_cursor.Author">
148+
<constructor>
149+
<idArg column="id" javaType="int" />
150+
<arg column="name" javaType="string" />
151+
<arg column="books" javaType="list"
152+
typeHandler="org.apache.ibatis.submitted.oracle_cursor.BooksTypeHandler" />
153+
</constructor>
154+
</resultMap>
155+
156+
<select id="selectNestedCursorTypeHandlerConstructor"
157+
resultMap="authorRM_TypeHandlerConstructor">
158+
<include refid="nestedCursor" />
159+
</select>
160+
131161
<!-- Use inline result map to map nested cursor to collection. -->
132162
<resultMap id="author2RM_InlineRM"
133163
type="org.apache.ibatis.submitted.oracle_cursor.Author2">

0 commit comments

Comments
 (0)