001/* 002 * Copyright 2002-2018 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.jdbc.support; 018 019import java.util.Iterator; 020import java.util.LinkedList; 021import java.util.List; 022import java.util.Map; 023 024import org.springframework.dao.DataRetrievalFailureException; 025import org.springframework.dao.InvalidDataAccessApiUsageException; 026 027/** 028 * The standard implementation of the {@link KeyHolder} interface, to be used for 029 * holding auto-generated keys (as potentially returned by JDBC insert statements). 030 * 031 * <p>Create an instance of this class for each insert operation, and pass it 032 * to the corresponding {@link org.springframework.jdbc.core.JdbcTemplate} or 033 * {@link org.springframework.jdbc.object.SqlUpdate} methods. 034 * 035 * @author Thomas Risberg 036 * @author Juergen Hoeller 037 * @since 1.1 038 */ 039public class GeneratedKeyHolder implements KeyHolder { 040 041 private final List<Map<String, Object>> keyList; 042 043 044 /** 045 * Create a new GeneratedKeyHolder with a default list. 046 */ 047 public GeneratedKeyHolder() { 048 this.keyList = new LinkedList<Map<String, Object>>(); 049 } 050 051 /** 052 * Create a new GeneratedKeyHolder with a given list. 053 * @param keyList a list to hold maps of keys 054 */ 055 public GeneratedKeyHolder(List<Map<String, Object>> keyList) { 056 this.keyList = keyList; 057 } 058 059 060 @Override 061 public Number getKey() throws InvalidDataAccessApiUsageException, DataRetrievalFailureException { 062 if (this.keyList.isEmpty()) { 063 return null; 064 } 065 if (this.keyList.size() > 1 || this.keyList.get(0).size() > 1) { 066 throw new InvalidDataAccessApiUsageException( 067 "The getKey method should only be used when a single key is returned. " + 068 "The current key entry contains multiple keys: " + this.keyList); 069 } 070 Iterator<Object> keyIter = this.keyList.get(0).values().iterator(); 071 if (keyIter.hasNext()) { 072 Object key = keyIter.next(); 073 if (!(key instanceof Number)) { 074 throw new DataRetrievalFailureException( 075 "The generated key is not of a supported numeric type. " + 076 "Unable to cast [" + (key != null ? key.getClass().getName() : null) + 077 "] to [" + Number.class.getName() + "]"); 078 } 079 return (Number) key; 080 } 081 else { 082 throw new DataRetrievalFailureException("Unable to retrieve the generated key. " + 083 "Check that the table has an identity column enabled."); 084 } 085 } 086 087 @Override 088 public Map<String, Object> getKeys() throws InvalidDataAccessApiUsageException { 089 if (this.keyList.isEmpty()) { 090 return null; 091 } 092 if (this.keyList.size() > 1) { 093 throw new InvalidDataAccessApiUsageException( 094 "The getKeys method should only be used when keys for a single row are returned. " + 095 "The current key list contains keys for multiple rows: " + this.keyList); 096 } 097 return this.keyList.get(0); 098 } 099 100 @Override 101 public List<Map<String, Object>> getKeyList() { 102 return this.keyList; 103 } 104 105}