View Javadoc
1   /*
2    * Copyright 2002-2012 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.jdbc.support;
18  
19  import java.util.Iterator;
20  import java.util.LinkedList;
21  import java.util.List;
22  import java.util.Map;
23  
24  import org.springframework.dao.DataRetrievalFailureException;
25  import org.springframework.dao.InvalidDataAccessApiUsageException;
26  
27  /**
28   * Default implementation of the {@link KeyHolder} interface, to be used for
29   * holding auto-generated keys (as potentially returned by JDBC insert statements).
30   *
31   * <p>Create an instance of this class for each insert operation, and pass
32   * it to the corresponding {@link org.springframework.jdbc.core.JdbcTemplate}
33   * or {org.springframework.jdbc.object.SqlUpdate} methods.
34   *
35   * @author Thomas Risberg
36   * @author Juergen Hoeller
37   * @since 1.1
38   */
39  public class GeneratedKeyHolder implements KeyHolder {
40  
41  	private final List<Map<String, Object>> keyList;
42  
43  
44  	/**
45  	 * Create a new GeneratedKeyHolder with a default list.
46  	 */
47  	public GeneratedKeyHolder() {
48  		this.keyList = new LinkedList<Map<String, Object>>();
49  	}
50  
51  	/**
52  	 * Create a new GeneratedKeyHolder with a given list.
53  	 * @param keyList a list to hold maps of keys
54  	 */
55  	public GeneratedKeyHolder(List<Map<String, Object>> keyList) {
56  		this.keyList = keyList;
57  	}
58  
59  
60  	@Override
61  	public Number getKey() throws InvalidDataAccessApiUsageException, DataRetrievalFailureException {
62  		if (this.keyList.size() == 0) {
63  			return null;
64  		}
65  		if (this.keyList.size() > 1 || this.keyList.get(0).size() > 1) {
66  			throw new InvalidDataAccessApiUsageException(
67  					"The getKey method should only be used when a single key is returned.  " +
68  					"The current key entry contains multiple keys: " + this.keyList);
69  		}
70  		Iterator<Object> keyIter = this.keyList.get(0).values().iterator();
71  		if (keyIter.hasNext()) {
72  			Object key = keyIter.next();
73  			if (!(key instanceof Number)) {
74  				throw new DataRetrievalFailureException(
75  						"The generated key is not of a supported numeric type. " +
76  						"Unable to cast [" + (key != null ? key.getClass().getName() : null) +
77  						"] to [" + Number.class.getName() + "]");
78  			}
79  			return (Number) key;
80  		}
81  		else {
82  			throw new DataRetrievalFailureException("Unable to retrieve the generated key. " +
83  					"Check that the table has an identity column enabled.");
84  		}
85  	}
86  
87  	@Override
88  	public Map<String, Object> getKeys() throws InvalidDataAccessApiUsageException {
89  		if (this.keyList.size() == 0) {
90  			return null;
91  		}
92  		if (this.keyList.size() > 1)
93  			throw new InvalidDataAccessApiUsageException(
94  					"The getKeys method should only be used when keys for a single row are returned.  " +
95  					"The current key list contains keys for multiple rows: " + this.keyList);
96  		return this.keyList.get(0);
97  	}
98  
99  	@Override
100 	public List<Map<String, Object>> getKeyList() {
101 		return this.keyList;
102 	}
103 
104 }