1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
29
30
31
32
33
34
35
36
37
38
39 public class GeneratedKeyHolder implements KeyHolder {
40
41 private final List<Map<String, Object>> keyList;
42
43
44
45
46
47 public GeneratedKeyHolder() {
48 this.keyList = new LinkedList<Map<String, Object>>();
49 }
50
51
52
53
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 }