Skip to content

Commit cc3b510

Browse files
committed
Fixes #25. Check there is at least one result when executing the
selectKey statement
1 parent ad13be0 commit cc3b510

File tree

4 files changed

+43
-21
lines changed

4 files changed

+43
-21
lines changed

src/main/java/org/apache/ibatis/builder/annotation/MapperAnnotationBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ private KeyGenerator handleSelectKeyAnnotation(SelectKey selectKeyAnnotation, St
541541
SqlCommandType sqlCommandType = SqlCommandType.SELECT;
542542

543543
assistant.addMappedStatement(id, sqlSource, statementType, sqlCommandType, fetchSize, timeout, parameterMap, parameterTypeClass, resultMap, resultTypeClass, resultSetTypeEnum,
544-
flushCache, useCache, false, // TODO issue #577
544+
flushCache, useCache, false,
545545
keyGenerator, keyProperty, null, null, languageDriver);
546546

547547
id = assistant.applyCurrentNamespace(id, false);

src/main/java/org/apache/ibatis/executor/keygen/SelectKeyGenerator.java

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2012 The MyBatis Team
2+
* Copyright 2009-2013 The MyBatis Team
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.
@@ -27,6 +27,7 @@
2727
import org.apache.ibatis.session.RowBounds;
2828

2929
public class SelectKeyGenerator implements KeyGenerator {
30+
3031
public static final String SELECT_KEY_SUFFIX = "!selectKey";
3132
private boolean executeBefore;
3233
private MappedStatement keyStatement;
@@ -50,27 +51,24 @@ public void processAfter(Executor executor, MappedStatement ms, Statement stmt,
5051

5152
private void processGeneratedKeys(Executor executor, MappedStatement ms, Object parameter) {
5253
try {
53-
final Configuration configuration = ms.getConfiguration();
54-
if (parameter != null) {
55-
String keyStatementName = ms.getId() + SELECT_KEY_SUFFIX;
56-
if (configuration.hasStatement(keyStatementName)) {
57-
58-
if (keyStatement != null && keyStatement.getKeyProperties() != null) {
59-
String keyProperty = keyStatement.getKeyProperties()[0]; //just one key property is supported
60-
final MetaObject metaParam = configuration.newMetaObject(parameter);
61-
if (keyProperty != null && metaParam.hasSetter(keyProperty)) {
62-
// Do not close keyExecutor.
63-
// The transaction will be closed by parent executor.
64-
Executor keyExecutor = configuration.newExecutor(executor.getTransaction(), ExecutorType.SIMPLE);
65-
List<Object> values = keyExecutor.query(keyStatement, parameter, RowBounds.DEFAULT, Executor.NO_RESULT_HANDLER);
66-
if (values.size() > 1) {
67-
throw new ExecutorException("Select statement for SelectKeyGenerator returned more than one value.");
68-
}
69-
metaParam.setValue(keyProperty, values.get(0));
70-
}
54+
if (parameter != null && keyStatement != null && keyStatement.getKeyProperties() != null) {
55+
String keyProperty = keyStatement.getKeyProperties()[0]; // just one key property is supported
56+
final Configuration configuration = ms.getConfiguration();
57+
final MetaObject metaParam = configuration.newMetaObject(parameter);
58+
if (keyProperty != null && metaParam.hasSetter(keyProperty)) {
59+
// Do not close keyExecutor.
60+
// The transaction will be closed by parent executor.
61+
Executor keyExecutor = configuration.newExecutor(executor.getTransaction(), ExecutorType.SIMPLE);
62+
List<Object> values = keyExecutor.query(keyStatement, parameter, RowBounds.DEFAULT, Executor.NO_RESULT_HANDLER);
63+
if (values.size() == 1) {
64+
metaParam.setValue(keyProperty, values.get(0));
65+
} else if (values.size() > 1) {
66+
throw new ExecutorException("Select statement for SelectKeyGenerator returned more than one value.");
7167
}
7268
}
7369
}
70+
} catch (ExecutorException e) {
71+
throw e;
7472
} catch (Exception e) {
7573
throw new ExecutorException("Error selecting key or setting result to parameter object. Cause: " + e, e);
7674
}

src/test/java/org/apache/ibatis/submitted/selectkey/SelectKeyTest.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import org.apache.ibatis.session.SqlSessionFactory;
2222
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
2323
import static org.junit.Assert.assertEquals;
24-
import static org.junit.Assert.assertNotNull;
24+
import static org.junit.Assert.*;
2525

2626
import org.junit.Before;
2727
import org.junit.Test;
@@ -107,6 +107,22 @@ public void testInsertTable2() {
107107
sqlSession.close();
108108
}
109109
}
110+
111+
@Test
112+
public void testInsertTable3() {
113+
SqlSession sqlSession = sqlSessionFactory.openSession();
114+
115+
try {
116+
Map<String, String> parms = new HashMap<String, String>();
117+
parms.put("name", "Fred");
118+
int rows = sqlSession.insert("org.apache.ibatis.submitted.selectkey.Table2.insertNoValuesInSelectKey", parms);
119+
assertEquals(1, rows);
120+
assertNull(parms.get("id"));
121+
122+
} finally {
123+
sqlSession.close();
124+
}
125+
}
110126

111127
@Test
112128
public void testAnnotatedInsertTable2() {

src/test/java/org/apache/ibatis/submitted/selectkey/Table2.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,12 @@
2424
CALL IDENTITY()
2525
</selectKey>
2626
</insert>
27+
28+
<insert id="insertNoValuesInSelectKey" parameterType="map">
29+
insert into table2 (name) values (#{name})
30+
<selectKey resultType="java.lang.Integer" keyProperty="id">
31+
select * from table2 where name = 'xxx'
32+
</selectKey>
33+
</insert>
34+
2735
</mapper>

0 commit comments

Comments
 (0)