1
1
/*
2
- * Copyright 2009-2012 The MyBatis Team
2
+ * Copyright 2009-2013 The MyBatis Team
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
27
27
import org .apache .ibatis .session .RowBounds ;
28
28
29
29
public class SelectKeyGenerator implements KeyGenerator {
30
+
30
31
public static final String SELECT_KEY_SUFFIX = "!selectKey" ;
31
32
private boolean executeBefore ;
32
33
private MappedStatement keyStatement ;
@@ -50,27 +51,24 @@ public void processAfter(Executor executor, MappedStatement ms, Statement stmt,
50
51
51
52
private void processGeneratedKeys (Executor executor , MappedStatement ms , Object parameter ) {
52
53
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." );
71
67
}
72
68
}
73
69
}
70
+ } catch (ExecutorException e ) {
71
+ throw e ;
74
72
} catch (Exception e ) {
75
73
throw new ExecutorException ("Error selecting key or setting result to parameter object. Cause: " + e , e );
76
74
}
0 commit comments