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.
2727import org .apache .ibatis .session .RowBounds ;
2828
2929public 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 }
0 commit comments