added check for existing configuration of generated keys before calling update with a KeyHolder. (SPR-7564)

This commit is contained in:
Thomas Risberg 2011-06-06 00:52:01 +00:00
parent b06b5502e0
commit 97e047ed66
1 changed files with 7 additions and 0 deletions

View File

@ -20,6 +20,7 @@ import java.util.Map;
import javax.sql.DataSource;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.jdbc.JdbcUpdateAffectedIncorrectNumberOfRowsException;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterUtils;
@ -177,6 +178,12 @@ public class SqlUpdate extends SqlOperation {
* @return the number of rows affected by the update
*/
public int update(Object[] params, KeyHolder generatedKeyHolder) throws DataAccessException {
if (!isReturnGeneratedKeys() && getGeneratedKeysColumnNames() == null) {
throw new InvalidDataAccessApiUsageException(
"The update method taking a KeyHolder should only be used when generated keys have " +
"been configured by calling either 'setReturnGeneratedKeys' or " +
"'setGeneratedKeysColumnNames'.");
}
validateParameters(params);
int rowsAffected = getJdbcTemplate().update(newPreparedStatementCreator(params), generatedKeyHolder);
checkRowsAffected(rowsAffected);