Skip to content

Commit

Permalink
Related to mybatis#25. Ensure selectKey returns one value.
Browse files Browse the repository at this point in the history
  • Loading branch information
emacarron committed Apr 2, 2013
1 parent b38da71 commit 03936d6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,12 @@ private void processGeneratedKeys(Executor executor, MappedStatement ms, Object
// The transaction will be closed by parent executor.
Executor keyExecutor = configuration.newExecutor(executor.getTransaction(), ExecutorType.SIMPLE);
List<Object> values = keyExecutor.query(keyStatement, parameter, RowBounds.DEFAULT, Executor.NO_RESULT_HANDLER);
if (values.size() == 1) {
metaParam.setValue(keyProperty, values.get(0));
if (values.size() == 0) {
throw new ExecutorException("SelectKey returned no data.");
} else if (values.size() > 1) {
throw new ExecutorException("Select statement for SelectKeyGenerator returned more than one value.");
throw new ExecutorException("SelectKey returned more than one value.");
} else {
metaParam.setValue(keyProperty, values.get(0));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.apache.ibatis.submitted.selectkey;

import org.apache.ibatis.exceptions.PersistenceException;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.jdbc.ScriptRunner;
import org.apache.ibatis.session.SqlSession;
Expand Down Expand Up @@ -108,22 +109,33 @@ public void testInsertTable2() {
}
}

@Test
public void testInsertTable3() {
@Test(expected=PersistenceException.class)
public void testSeleckKeyReturnsNoData() {
SqlSession sqlSession = sqlSessionFactory.openSession();

try {
Map<String, String> parms = new HashMap<String, String>();
parms.put("name", "Fred");
int rows = sqlSession.insert("org.apache.ibatis.submitted.selectkey.Table2.insertNoValuesInSelectKey", parms);
assertEquals(1, rows);
assertNull(parms.get("id"));
} finally {
sqlSession.close();
}
}

@Test(expected=PersistenceException.class)
public void testSeleckKeyReturnsTooManyData() {
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
Map<String, String> parms = new HashMap<String, String>();
parms.put("name", "Fred");
sqlSession.insert("org.apache.ibatis.submitted.selectkey.Table2.insertTooManyValuesInSelectKey", parms);
sqlSession.insert("org.apache.ibatis.submitted.selectkey.Table2.insertTooManyValuesInSelectKey", parms);
} finally {
sqlSession.close();
}
}

@Test
public void testAnnotatedInsertTable2() {
SqlSession sqlSession = sqlSessionFactory.openSession();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,12 @@
select * from table2 where name = 'xxx'
</selectKey>
</insert>

<insert id="insertTooManyValuesInSelectKey" parameterType="map">
insert into table2 (name) values (#{name})
<selectKey resultType="java.lang.Integer" keyProperty="id">
select id from table2
</selectKey>
</insert>

</mapper>

0 comments on commit 03936d6

Please sign in to comment.