Skip to content

Commit 0803167

Browse files
committed
Use jdbcType="CURSOR" for indicator; removed NESTED_CURSOR
1 parent 571af93 commit 0803167

File tree

15 files changed

+95
-127
lines changed

15 files changed

+95
-127
lines changed

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

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ private boolean applyPropertyMappings(ResultSetWrapper rsw, ResultMap resultMap,
532532
final List<ResultMapping> propertyMappings = resultMap.getPropertyResultMappings();
533533
for (ResultMapping propertyMapping : propertyMappings) {
534534
String column = prependPrefix(propertyMapping.getColumn(), columnPrefix);
535-
if (propertyMapping.getNestedResultMapId() != null) {
535+
if (propertyMapping.getNestedResultMapId() != null && !JdbcType.CURSOR.equals(propertyMapping.getJdbcType())) {
536536
// the user added a column attribute to a nested result map, ignore it
537537
column = null;
538538
}
@@ -568,28 +568,33 @@ private Object getPropertyMappingValue(ResultSet rs, MetaObject metaResultObject
568568
if (propertyMapping.getNestedQueryId() != null) {
569569
return getNestedQueryMappingValue(rs, metaResultObject, propertyMapping, lazyLoader, columnPrefix);
570570
}
571+
if (JdbcType.CURSOR.equals(propertyMapping.getJdbcType())) {
572+
return getNestedCursorValue(rs, metaResultObject, propertyMapping, columnPrefix);
573+
}
571574
if (propertyMapping.getResultSet() != null) {
572-
if (ResultMapping.NESTED_CURSOR.equals(propertyMapping.getResultSet())) {
573-
final String column = prependPrefix(propertyMapping.getColumn(), columnPrefix);
574-
ResultMap nestedResultMap = resolveDiscriminatedResultMap(rs,
575-
configuration.getResultMap(propertyMapping.getNestedResultMapId()),
576-
getColumnPrefix(columnPrefix, propertyMapping));
577-
ResultSetWrapper rsw = new ResultSetWrapper(rs.getObject(column, ResultSet.class), configuration);
578-
List<Object> results = new ArrayList<>();
579-
handleResultSet(rsw, nestedResultMap, results, null);
580-
linkObjects(metaResultObject, propertyMapping, results.get(0), true);
581-
return metaResultObject.getValue(propertyMapping.getProperty());
582-
} else {
583-
addPendingChildRelation(rs, metaResultObject, propertyMapping); // TODO is that OK?
584-
return DEFERRED;
585-
}
575+
addPendingChildRelation(rs, metaResultObject, propertyMapping); // TODO is that OK?
576+
return DEFERRED;
586577
} else {
587578
final TypeHandler<?> typeHandler = propertyMapping.getTypeHandler();
588579
final String column = prependPrefix(propertyMapping.getColumn(), columnPrefix);
589580
return typeHandler.getResult(rs, column);
590581
}
591582
}
592583

584+
private Object getNestedCursorValue(ResultSet rs, MetaObject metaResultObject, ResultMapping propertyMapping,
585+
String parentColumnPrefix) throws SQLException {
586+
final String column = prependPrefix(propertyMapping.getColumn(), parentColumnPrefix);
587+
final String property = propertyMapping.getProperty();
588+
ResultMap nestedResultMap = resolveDiscriminatedResultMap(rs,
589+
configuration.getResultMap(propertyMapping.getNestedResultMapId()),
590+
getColumnPrefix(parentColumnPrefix, propertyMapping));
591+
ResultSetWrapper rsw = new ResultSetWrapper(rs.getObject(column, ResultSet.class), configuration);
592+
List<Object> results = new ArrayList<>();
593+
handleResultSet(rsw, nestedResultMap, results, null);
594+
linkObjects(metaResultObject, propertyMapping, results.get(0), true);
595+
return metaResultObject.getValue(property);
596+
}
597+
593598
private List<UnMappedColumnAutoMapping> createAutomaticMappings(ResultSetWrapper rsw, ResultMap resultMap,
594599
MetaObject metaObject, String columnPrefix) throws SQLException {
595600
final String mapKey = resultMap.getId() + ":" + columnPrefix;

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package org.apache.ibatis.mapping;
1717

1818
import java.util.ArrayList;
19-
import java.util.Arrays;
2019
import java.util.Collections;
2120
import java.util.List;
2221

@@ -204,11 +203,6 @@ public MappedStatement build() {
204203
assert mappedStatement.id != null;
205204
assert mappedStatement.sqlSource != null;
206205
assert mappedStatement.lang != null;
207-
if (mappedStatement.resultSets != null
208-
&& Arrays.asList(mappedStatement.resultSets).contains(ResultMapping.NESTED_CURSOR)) {
209-
throw new IllegalStateException(
210-
"Result set name '" + ResultMapping.NESTED_CURSOR + "' is reserved, please assign another name.");
211-
}
212206
mappedStatement.resultMaps = Collections.unmodifiableList(mappedStatement.resultMaps);
213207
return mappedStatement;
214208
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2024 the original author or authors.
2+
* Copyright 2009-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,6 +30,7 @@
3030
import org.apache.ibatis.logging.LogFactory;
3131
import org.apache.ibatis.reflection.ParamNameUtil;
3232
import org.apache.ibatis.session.Configuration;
33+
import org.apache.ibatis.type.JdbcType;
3334

3435
/**
3536
* @author Clinton Begin
@@ -93,8 +94,8 @@ public ResultMap build() {
9394
final List<String> constructorArgNames = new ArrayList<>();
9495
for (ResultMapping resultMapping : resultMap.resultMappings) {
9596
resultMap.hasNestedQueries = resultMap.hasNestedQueries || resultMapping.getNestedQueryId() != null;
96-
resultMap.hasNestedResultMaps = resultMap.hasNestedResultMaps
97-
|| resultMapping.getNestedResultMapId() != null && resultMapping.getResultSet() == null;
97+
resultMap.hasNestedResultMaps = resultMap.hasNestedResultMaps || resultMapping.getNestedResultMapId() != null
98+
&& resultMapping.getResultSet() == null && !JdbcType.CURSOR.equals(resultMapping.getJdbcType());
9899
final String column = resultMapping.getColumn();
99100
if (column != null) {
100101
resultMap.mappedColumns.add(column.toUpperCase(Locale.ENGLISH));

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,6 @@
3030
*/
3131
public class ResultMapping {
3232

33-
/**
34-
* Reserved result set name indicating a nested cursor is mapped to this property.
35-
*/
36-
public static final String NESTED_CURSOR = "NESTED_CURSOR";
37-
3833
private Configuration configuration;
3934
private String property;
4035
private String column;
@@ -171,8 +166,7 @@ private void validate() {
171166
if (resultMapping.foreignColumn != null) {
172167
numForeignColumns = resultMapping.foreignColumn.split(",").length;
173168
}
174-
if (numColumns != numForeignColumns && !NESTED_CURSOR.equals(resultMapping.resultSet)) {
175-
// Nested cursor does not use 'foreignKey'
169+
if (numColumns != numForeignColumns) {
176170
throw new IllegalStateException(
177171
"There should be the same number of columns and foreignColumns in property " + resultMapping.property);
178172
}

src/site/es/xdoc/sqlmap-xml.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,7 +1437,7 @@ Here is the statement and result map.</p>
14371437
<source><![CDATA[<resultMap id="blogResult" type="Blog">
14381438
<id property="id" column="id" />
14391439
<result property="title" column="title" />
1440-
<association property="author" column="author" resultSet="NESTED_CURSOR">
1440+
<association property="author" column="author" jdbcType="CURSOR">
14411441
<id property="id" column="id" />
14421442
<result property="username" column="username" />
14431443
</association>
@@ -1449,8 +1449,8 @@ Here is the statement and result map.</p>
14491449
) author from blog b
14501450
</select>]]></source>
14511451

1452-
<p>Compared to the examples in the previous section, the key difference is the <code>resultSet</code> attribute in the <code>&lt;association&gt;</code> element.<br />
1453-
Its value <code>NESTED_CURSOR</code> indicates that the value of the column <code>author</code> is nested cursor.</p>
1452+
<p>Compared to the examples in the previous section, the key difference is the <code>jdbcType</code> attribute in the <code>&lt;association&gt;</code> element.<br />
1453+
Its value <code>CURSOR</code> indicates that the value of the column <code>author</code> is nested cursor.</p>
14541454

14551455

14561456
<h4>ResultSets múltiples en Association</h4>
@@ -1630,12 +1630,12 @@ SELECT * FROM AUTHOR WHERE ID = #{id}]]></source>
16301630
<h4>Nested Cursor for Collection</h4>
16311631

16321632
<p>It might be obvious, but nested cursor can return multiple rows.<br />
1633-
Just like <code>&lt;association&gt;</code>, you just need to specify <code>resultSet="NESTED_CURSOR"</code> in the <code>&lt;collection&gt;</code> element.</p>
1633+
Just like <code>&lt;association&gt;</code>, you just need to specify <code>jdbcType="CURSOR"</code> in the <code>&lt;collection&gt;</code> element.</p>
16341634

16351635
<source><![CDATA[<resultMap id="blogResult" type="Blog">
16361636
<id property="id" column="id" />
16371637
<result property="title" column="title" />
1638-
<collection property="posts" column="posts" resultSet="NESTED_CURSOR">
1638+
<collection property="posts" column="posts" jdbcType="CURSOR">
16391639
<id property="id" column="id" />
16401640
<result property="subject" column="subject" />
16411641
<result property="body" column="body" />

src/site/ja/xdoc/sqlmap-xml.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1617,7 +1617,7 @@ User{username=Peter, roles=[Users, Maintainers, Approvers]}
16171617
<source><![CDATA[<resultMap id="blogResult" type="Blog">
16181618
<id property="id" column="id" />
16191619
<result property="title" column="title" />
1620-
<association property="author" column="author" resultSet="NESTED_CURSOR">
1620+
<association property="author" column="author" jdbcType="CURSOR">
16211621
<id property="id" column="id" />
16221622
<result property="username" column="username" />
16231623
</association>
@@ -1629,7 +1629,7 @@ User{username=Peter, roles=[Users, Maintainers, Approvers]}
16291629
) author from blog b
16301630
</select>]]></source>
16311631

1632-
<p>上の章の例との重要な違いは、<code>&lt;association&gt;</code> 要素の <code>resultSet</code> 属性に特別な値 <code>NESTED_CURSOR</code> を指定している点です。<br />
1632+
<p>上の章の例との重要な違いは、<code>&lt;association&gt;</code> 要素の <code>jdbcType</code> 属性に <code>CURSOR</code> を指定している点です。<br />
16331633
これによって、<code>author</code> 列の値をネストされたカーソルとしてマッピングすることができます。</p>
16341634

16351635

@@ -1818,12 +1818,12 @@ SELECT * FROM AUTHOR WHERE ID = #{id}]]></source>
18181818
<h4>ネストされたカーソルを collection にマッピングする</h4>
18191819

18201820
<p>当然ですが、ネストされたカーソルが複数の値を返す場合もあります。<br />
1821-
先に説明した <code>&lt;association&gt;</code> の場合と同様、 <code>&lt;collection&gt;</code> 要素に <code>resultSet="NESTED_CURSOR"</code> を指定してください。</p>
1821+
先に説明した <code>&lt;association&gt;</code> の場合と同様、 <code>&lt;collection&gt;</code> 要素に <code>jdbcType="CURSOR"</code> を指定してください。</p>
18221822

18231823
<source><![CDATA[<resultMap id="blogResult" type="Blog">
18241824
<id property="id" column="id" />
18251825
<result property="title" column="title" />
1826-
<collection property="posts" column="posts" resultSet="NESTED_CURSOR">
1826+
<collection property="posts" column="posts" jdbcType="CURSOR">
18271827
<id property="id" column="id" />
18281828
<result property="subject" column="subject" />
18291829
<result property="body" column="body" />

src/site/ko/xdoc/sqlmap-xml.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1464,7 +1464,7 @@ Here is the statement and result map.</p>
14641464
<source><![CDATA[<resultMap id="blogResult" type="Blog">
14651465
<id property="id" column="id" />
14661466
<result property="title" column="title" />
1467-
<association property="author" column="author" resultSet="NESTED_CURSOR">
1467+
<association property="author" column="author" jdbcType="CURSOR">
14681468
<id property="id" column="id" />
14691469
<result property="username" column="username" />
14701470
</association>
@@ -1476,8 +1476,8 @@ Here is the statement and result map.</p>
14761476
) author from blog b
14771477
</select>]]></source>
14781478

1479-
<p>Compared to the examples in the previous section, the key difference is the <code>resultSet</code> attribute in the <code>&lt;association&gt;</code> element.<br />
1480-
Its value <code>NESTED_CURSOR</code> indicates that the value of the column <code>author</code> is nested cursor.</p>
1479+
<p>Compared to the examples in the previous section, the key difference is the <code>jdbcType</code> attribute in the <code>&lt;association&gt;</code> element.<br />
1480+
Its value <code>CURSOR</code> indicates that the value of the column <code>author</code> is nested cursor.</p>
14811481

14821482

14831483
<h4>Multiple ResultSets for Association</h4>
@@ -1662,12 +1662,12 @@ SELECT * FROM AUTHOR WHERE ID = #{id}]]></source>
16621662
<h4>Nested Cursor for Collection</h4>
16631663

16641664
<p>It might be obvious, but nested cursor can return multiple rows.<br />
1665-
Just like <code>&lt;association&gt;</code>, you just need to specify <code>resultSet="NESTED_CURSOR"</code> in the <code>&lt;collection&gt;</code> element.</p>
1665+
Just like <code>&lt;association&gt;</code>, you just need to specify <code>jdbcType="CURSOR"</code> in the <code>&lt;collection&gt;</code> element.</p>
16661666

16671667
<source><![CDATA[<resultMap id="blogResult" type="Blog">
16681668
<id property="id" column="id" />
16691669
<result property="title" column="title" />
1670-
<collection property="posts" column="posts" resultSet="NESTED_CURSOR">
1670+
<collection property="posts" column="posts" jdbcType="CURSOR">
16711671
<id property="id" column="id" />
16721672
<result property="subject" column="subject" />
16731673
<result property="body" column="body" />

src/site/markdown/sqlmap-xml.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -948,7 +948,7 @@ Here is the statement and result map.
948948
<resultMap id="blogResult" type="Blog">
949949
<id property="id" column="id" />
950950
<result property="title" column="title" />
951-
<association property="author" column="author" resultSet="NESTED_CURSOR">
951+
<association property="author" column="author" jdbcType="CURSOR">
952952
<id property="id" column="id" />
953953
<result property="username" column="username" />
954954
</association>
@@ -961,8 +961,9 @@ Here is the statement and result map.
961961
</select>
962962
```
963963

964-
Compared to the examples in the previous section, the key difference is the `resultSet` attribute in the `<association>` element.
965-
Its value `NESTED_CURSOR` indicates that the value of the column `author` is nested cursor.
964+
Compared to the examples in the previous section, the key difference is the `jdbcType` attribute in the `<association>` element.
965+
Its value `CURSOR` indicates that the value of the column `author` is nested cursor.
966+
966967

967968
#### Multiple ResultSets for Association
968969

@@ -1009,6 +1010,7 @@ Now we can specify that the data to fill the "author" association comes in the "
10091010
</resultMap>
10101011
```
10111012

1013+
10121014
You've seen above how to deal with a "has one" type association. But what about "has many"? That's the subject of the next section.
10131015

10141016
#### collection
@@ -1119,13 +1121,13 @@ Also, if you prefer the longer form that allows for more reusability of your res
11191121
#### Nested Cursor for Collection
11201122

11211123
It might be obvious, but nested cursor can return multiple rows.
1122-
Just like `<association>`, you just need to specify `resultSet="NESTED_CURSOR"` in the `<collection>` element.
1124+
Just like `<association>`, you just need to specify `jdbcType="CURSOR""` in the `<collection>` element.
11231125

11241126
```xml
11251127
<resultMap id="blogResult" type="Blog">
11261128
<id property="id" column="id" />
11271129
<result property="title" column="title" />
1128-
<collection property="posts" column="posts" resultSet="NESTED_CURSOR">
1130+
<collection property="posts" column="posts" jdbcType="CURSOR">
11291131
<id property="id" column="id" />
11301132
<result property="subject" column="subject" />
11311133
<result property="body" column="body" />

src/site/zh_CN/xdoc/sqlmap-xml.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1669,7 +1669,7 @@ Here is the statement and result map.</p>
16691669
<source><![CDATA[<resultMap id="blogResult" type="Blog">
16701670
<id property="id" column="id" />
16711671
<result property="title" column="title" />
1672-
<association property="author" column="author" resultSet="NESTED_CURSOR">
1672+
<association property="author" column="author" jdbcType="CURSOR">
16731673
<id property="id" column="id" />
16741674
<result property="username" column="username" />
16751675
</association>
@@ -1681,8 +1681,8 @@ Here is the statement and result map.</p>
16811681
) author from blog b
16821682
</select>]]></source>
16831683

1684-
<p>Compared to the examples in the previous section, the key difference is the <code>resultSet</code> attribute in the <code>&lt;association&gt;</code> element.<br />
1685-
Its value <code>NESTED_CURSOR</code> indicates that the value of the column <code>author</code> is nested cursor.</p>
1684+
<p>Compared to the examples in the previous section, the key difference is the <code>jdbcType</code> attribute in the <code>&lt;association&gt;</code> element.<br />
1685+
Its value <code>CURSOR</code> indicates that the value of the column <code>author</code> is nested cursor.</p>
16861686

16871687

16881688
<h4>关联的多结果集(ResultSet)</h4>
@@ -1879,12 +1879,12 @@ SELECT * FROM AUTHOR WHERE ID = #{id}]]></source>
18791879
<h4>Nested Cursor for Collection</h4>
18801880

18811881
<p>It might be obvious, but nested cursor can return multiple rows.<br />
1882-
Just like <code>&lt;association&gt;</code>, you just need to specify <code>resultSet="NESTED_CURSOR"</code> in the <code>&lt;collection&gt;</code> element.</p>
1882+
Just like <code>&lt;association&gt;</code>, you just need to specify <code>jdbcType="CURSOR"</code> in the <code>&lt;collection&gt;</code> element.</p>
18831883

18841884
<source><![CDATA[<resultMap id="blogResult" type="Blog">
18851885
<id property="id" column="id" />
18861886
<result property="title" column="title" />
1887-
<collection property="posts" column="posts" resultSet="NESTED_CURSOR">
1887+
<collection property="posts" column="posts" jdbcType="CURSOR">
18881888
<id property="id" column="id" />
18891889
<result property="subject" column="subject" />
18901890
<result property="body" column="body" />

src/test/java/org/apache/ibatis/mapping/MappedStatementTest.java

Lines changed: 0 additions & 58 deletions
This file was deleted.

0 commit comments

Comments
 (0)