Top Description Fields Constructors Methods
org.springframework.jdbc.core.simple

public abstract Class AbstractJdbcInsert

extends Object
Class Inheritance
Known Direct Subclasses
org.springframework.jdbc.core.simple.SimpleJdbcInsert
Imports
java.sql.Connection, .PreparedStatement, .ResultSet, .SQLException, .Statement, java.util.ArrayList, .Arrays, .Collections, .HashMap, .List, .Map, javax.sql.DataSource, org.apache.commons.logging.Log, .LogFactory, org.springframework.dao.DataIntegrityViolationException, .InvalidDataAccessApiUsageException, .InvalidDataAccessResourceUsageException, org.springframework.jdbc.core.BatchPreparedStatementSetter, .ConnectionCallback, .JdbcTemplate, .SqlTypeValue, .StatementCreatorUtils, org.springframework.jdbc.core.metadata.TableMetaDataContext, org.springframework.jdbc.core.namedparam.SqlParameterSource, org.springframework.jdbc.support.GeneratedKeyHolder, .JdbcUtils, .KeyHolder, org.springframework.lang.Nullable, org.springframework.util.Assert

Abstract class to provide base functionality for easy (batch) inserts based on configuration options and database meta-data.

This class provides the processing arrangement for SimpleJdbcInsert.

Authors
Thomas Risberg, Juergen Hoeller, Sam Brannen
Since
2.5

Field Summary

Modifier and TypeField and Description
private volatile boolean
compiled

Has this operation been compiled?

private final List<String>
declaredColumns

List of column names to be used in insert statement.

private String[]
generatedKeyNames

The names of the columns holding the generated key.

private String
insertString

The generated string used for insert statement.

private int[]
insertTypes

The SQL type information for the insert columns.

private final JdbcTemplate
jdbcTemplate

Lower-level class used to execute SQL.

protected final Log
logger

Logger available to subclasses.

private final TableMetaDataContext
tableMetaDataContext

Context used to retrieve and manage database meta-data.

Constructor Summary

AccessConstructor and Description
protected
AbstractJdbcInsert(DataSource
the DataSource to be used
dataSource
)

Constructor to be used when initializing using a DataSource.

protected
AbstractJdbcInsert(JdbcTemplate
the JdbcTemplate to use
jdbcTemplate
)

Constructor to be used when initializing using a JdbcTemplate.

Method Summary

Modifier and TypeMethod and Description
protected void
checkCompiled()

Check whether this operation has been compiled already; lazily compile it if not already compiled.

protected void
checkIfConfigurationModificationIsAllowed()

Method to check whether we are allowed to make any configuration changes at this time.

public final synchronized void
compile()

Compile this JdbcInsert using provided parameters and meta-data plus other settings.

protected void
compileInternal()

Delegate method to perform the actual compilation.

protected int

Returns:

the number of rows affected
doExecute
(Map<String, ?>
a Map with parameter names and values to be used in insert
args
)

Delegate method that executes the insert using the passed-in Map of parameters.

protected int

Returns:

the number of rows affected
doExecute
(SqlParameterSource
parameter names and values to be used in insert
parameterSource
)

Delegate method that executes the insert using the passed-in SqlParameterSource.

protected Number

Returns:

the key generated by the insert
doExecuteAndReturnKey
(Map<String, ?>
a Map with parameter names and values to be used in insert
args
)

Method that provides execution of the insert using the passed-in Map of parameters and returning a generated key.

protected Number

Returns:

the key generated by the insert
doExecuteAndReturnKey
(SqlParameterSource
parameter names and values to be used in insert
parameterSource
)

Method that provides execution of the insert using the passed-in SqlParameterSource and returning a generated key.

protected KeyHolder

Returns:

the KeyHolder containing keys generated by the insert
doExecuteAndReturnKeyHolder
(Map<String, ?>
a Map with parameter names and values to be used in insert
args
)

Method that provides execution of the insert using the passed-in Map of parameters and returning all generated keys.

protected KeyHolder

Returns:

the KeyHolder containing keys generated by the insert
doExecuteAndReturnKeyHolder
(SqlParameterSource
parameter names and values to be used in insert
parameterSource
)

Method that provides execution of the insert using the passed-in SqlParameterSource and returning all generated keys.

protected int[]

Returns:

an array of number of rows affected
doExecuteBatch
(Map<String, ?>...
maps with parameter names and values to be used in the batch insert
batch
)

Delegate method that executes a batch insert using the passed-in Maps of parameters.

protected int[]

Returns:

an array of number of rows affected
doExecuteBatch
(SqlParameterSource...
parameter sources with names and values to be used in the batch insert
batch
)

Delegate method that executes a batch insert using the passed-in SqlParameterSources.

private int[]
executeBatchInternal(final List<List<Object>> batchValues)

Delegate method to execute the batch insert.

private KeyHolder
executeInsertAndReturnKeyHolderInternal(List<?> values)

Delegate method to execute the insert, generating any number of keys.

private Number
executeInsertAndReturnKeyInternal(List<?> values)

Delegate method to execute the insert, generating a single key.

private int
executeInsertInternal(List<?> values)

Delegate method to execute the insert.

public String
getCatalogName()

Get the name of the catalog for this insert.

public List<String>
getColumnNames()

Get the names of the columns used.

public String[]
getGeneratedKeyNames()

Get the names of any generated keys.

public String
getInsertString()

Get the insert string to be used.

public int[]
getInsertTypes()

Get the array of java.sql.Types to be used for insert.

public JdbcTemplate
getJdbcTemplate()

Get the configured JdbcTemplate.

public String
getSchemaName()

Get the name of the schema for this insert.

public String
getTableName()

Get the name of the table for this insert.

public boolean

Returns:

whether this operation is compiled and ready to use
isCompiled
()

Is this operation "compiled"?

public boolean
isQuoteIdentifiers()

Get the quoteIdentifiers flag.

protected List<Object>

Returns:

a List of values
matchInParameterValuesWithInsertColumns
(SqlParameterSource
the parameter values provided as a SqlParameterSource
parameterSource
)

Match the provided in parameter values with registered parameters and parameters defined via meta-data processing.

protected List<Object>

Returns:

a List of values
matchInParameterValuesWithInsertColumns
(Map<String, ?>
the parameter values provided as a Map
args
)

Match the provided in parameter values with registered parameters and parameters defined via meta-data processing.

protected void
onCompileInternal()

Hook method that subclasses may override to react to compilation.

private PreparedStatement

Returns:

the PreparedStatement
prepareStatementForGeneratedKeys
(Connection
the Connection to use
con
)

Create a PreparedStatement to be used for an insert operation with generated keys.

public void
setAccessTableColumnMetaData(boolean accessTableColumnMetaData)

Specify whether the parameter meta-data for the call should be used.

public void
setCatalogName(String catalogName)

Set the name of the catalog for this insert.

public void
setColumnNames(List<String> columnNames)

Set the names of the columns to be used.

public void
setGeneratedKeyName(String generatedKeyName)

Specify the name of a single generated key column.

public void
setGeneratedKeyNames(String... generatedKeyNames)

Set the names of any generated keys.

public void
setOverrideIncludeSynonymsDefault(boolean override)

Specify whether the default for including synonyms should be changed.

private void
setParameterValues(PreparedStatement
the PreparedStatement
preparedStatement
,
List<?>
the values to be set
values
,
int... columnTypes)

Internal implementation for setting parameter values.

public void
setQuoteIdentifiers(boolean
whether identifiers should be quoted
quoteIdentifiers
)

Specify whether SQL identifiers should be quoted.

public void
setSchemaName(String schemaName)

Set the name of the schema for this insert.

public void
setTableName(String tableName)

Set the name of the table for this insert.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

compiledback to summary
private volatile boolean compiled

Has this operation been compiled? Compilation means at least checking that a DataSource or JdbcTemplate has been provided.

declaredColumnsback to summary
private final List<String> declaredColumns

List of column names to be used in insert statement.

generatedKeyNamesback to summary
private String[] generatedKeyNames

The names of the columns holding the generated key.

insertStringback to summary
private String insertString

The generated string used for insert statement.

insertTypesback to summary
private int[] insertTypes

The SQL type information for the insert columns.

jdbcTemplateback to summary
private final JdbcTemplate jdbcTemplate

Lower-level class used to execute SQL.

loggerback to summary
protected final Log logger

Logger available to subclasses.

tableMetaDataContextback to summary
private final TableMetaDataContext tableMetaDataContext

Context used to retrieve and manage database meta-data.

Constructor Detail

AbstractJdbcInsertback to summary
protected AbstractJdbcInsert(DataSource dataSource)

Constructor to be used when initializing using a DataSource.

Parameters
dataSource:DataSource

the DataSource to be used

AbstractJdbcInsertback to summary
protected AbstractJdbcInsert(JdbcTemplate jdbcTemplate)

Constructor to be used when initializing using a JdbcTemplate.

Parameters
jdbcTemplate:JdbcTemplate

the JdbcTemplate to use

Method Detail

checkCompiledback to summary
protected void checkCompiled()

Check whether this operation has been compiled already; lazily compile it if not already compiled.

Automatically called by all doExecute*(...) methods.

checkIfConfigurationModificationIsAllowedback to summary
protected void checkIfConfigurationModificationIsAllowed()

Method to check whether we are allowed to make any configuration changes at this time.

If the class has been compiled, then no further changes to the configuration are allowed.

compileback to summary
public final synchronized void compile() throws InvalidDataAccessApiUsageException

Compile this JdbcInsert using provided parameters and meta-data plus other settings. This finalizes the configuration for this object and subsequent attempts to compile are ignored. This will be implicitly called the first time an un-compiled insert is executed.

Exceptions
InvalidDataAccessApiUsageException:
if the object hasn't been correctly initialized, for example if no DataSource has been provided
compileInternalback to summary
protected void compileInternal()

Delegate method to perform the actual compilation.

Subclasses can override this template method to perform their own compilation. Invoked after this base class's compilation is complete.

doExecuteback to summary
protected int doExecute(Map<String, ?> args)

Delegate method that executes the insert using the passed-in Map of parameters.

Parameters
args:Map<String, ?>

a Map with parameter names and values to be used in insert

Returns:int

the number of rows affected

doExecuteback to summary
protected int doExecute(SqlParameterSource parameterSource)

Delegate method that executes the insert using the passed-in SqlParameterSource.

Parameters
parameterSource:SqlParameterSource

parameter names and values to be used in insert

Returns:int

the number of rows affected

doExecuteAndReturnKeyback to summary
protected Number doExecuteAndReturnKey(Map<String, ?> args)

Method that provides execution of the insert using the passed-in Map of parameters and returning a generated key.

Parameters
args:Map<String, ?>

a Map with parameter names and values to be used in insert

Returns:Number

the key generated by the insert

doExecuteAndReturnKeyback to summary
protected Number doExecuteAndReturnKey(SqlParameterSource parameterSource)

Method that provides execution of the insert using the passed-in SqlParameterSource and returning a generated key.

Parameters
parameterSource:SqlParameterSource

parameter names and values to be used in insert

Returns:Number

the key generated by the insert

doExecuteAndReturnKeyHolderback to summary
protected KeyHolder doExecuteAndReturnKeyHolder(Map<String, ?> args)

Method that provides execution of the insert using the passed-in Map of parameters and returning all generated keys.

Parameters
args:Map<String, ?>

a Map with parameter names and values to be used in insert

Returns:KeyHolder

the KeyHolder containing keys generated by the insert

doExecuteAndReturnKeyHolderback to summary
protected KeyHolder doExecuteAndReturnKeyHolder(SqlParameterSource parameterSource)

Method that provides execution of the insert using the passed-in SqlParameterSource and returning all generated keys.

Parameters
parameterSource:SqlParameterSource

parameter names and values to be used in insert

Returns:KeyHolder

the KeyHolder containing keys generated by the insert

doExecuteBatchback to summary
protected int[] doExecuteBatch(Map<String, ?>... batch)

Delegate method that executes a batch insert using the passed-in Maps of parameters.

Parameters
batch:Map<String, ?>[]

maps with parameter names and values to be used in the batch insert

Returns:int[]

an array of number of rows affected

Annotations
@SuppressWarnings:unchecked
doExecuteBatchback to summary
protected int[] doExecuteBatch(SqlParameterSource... batch)

Delegate method that executes a batch insert using the passed-in SqlParameterSources.

Parameters
batch:SqlParameterSource[]

parameter sources with names and values to be used in the batch insert

Returns:int[]

an array of number of rows affected

executeBatchInternalback to summary
private int[] executeBatchInternal(final List<List<Object>> batchValues)

Delegate method to execute the batch insert.

executeInsertAndReturnKeyHolderInternalback to summary
private KeyHolder executeInsertAndReturnKeyHolderInternal(List<?> values)

Delegate method to execute the insert, generating any number of keys.

executeInsertAndReturnKeyInternalback to summary
private Number executeInsertAndReturnKeyInternal(List<?> values)

Delegate method to execute the insert, generating a single key.

executeInsertInternalback to summary
private int executeInsertInternal(List<?> values)

Delegate method to execute the insert.

getCatalogNameback to summary
public String getCatalogName()

Get the name of the catalog for this insert.

Annotations
@Nullable
getColumnNamesback to summary
public List<String> getColumnNames()

Get the names of the columns used.

getGeneratedKeyNamesback to summary
public String[] getGeneratedKeyNames()

Get the names of any generated keys.

getInsertStringback to summary
public String getInsertString()

Get the insert string to be used.

getInsertTypesback to summary
public int[] getInsertTypes()

Get the array of java.sql.Types to be used for insert.

getJdbcTemplateback to summary
public JdbcTemplate getJdbcTemplate()

Get the configured JdbcTemplate.

getSchemaNameback to summary
public String getSchemaName()

Get the name of the schema for this insert.

Annotations
@Nullable
getTableNameback to summary
public String getTableName()

Get the name of the table for this insert.

Annotations
@Nullable
isCompiledback to summary
public boolean isCompiled()

Is this operation "compiled"?

Returns:boolean

whether this operation is compiled and ready to use

isQuoteIdentifiersback to summary
public boolean isQuoteIdentifiers()

Get the quoteIdentifiers flag.

Since
6.1
See Also
setQuoteIdentifiers(boolean)
matchInParameterValuesWithInsertColumnsback to summary
protected List<Object> matchInParameterValuesWithInsertColumns(SqlParameterSource parameterSource)

Match the provided in parameter values with registered parameters and parameters defined via meta-data processing.

Parameters
parameterSource:SqlParameterSource

the parameter values provided as a SqlParameterSource

Returns:List<Object>

a List of values

matchInParameterValuesWithInsertColumnsback to summary
protected List<Object> matchInParameterValuesWithInsertColumns(Map<String, ?> args)

Match the provided in parameter values with registered parameters and parameters defined via meta-data processing.

Parameters
args:Map<String, ?>

the parameter values provided as a Map

Returns:List<Object>

a List of values

onCompileInternalback to summary
protected void onCompileInternal()

Hook method that subclasses may override to react to compilation.

This implementation is empty.

prepareStatementForGeneratedKeysback to summary
private PreparedStatement prepareStatementForGeneratedKeys(Connection con) throws SQLException

Create a PreparedStatement to be used for an insert operation with generated keys.

Parameters
con:Connection

the Connection to use

Returns:PreparedStatement

the PreparedStatement

setAccessTableColumnMetaDataback to summary
public void setAccessTableColumnMetaData(boolean accessTableColumnMetaData)

Specify whether the parameter meta-data for the call should be used.

The default is true.

setCatalogNameback to summary
public void setCatalogName(String catalogName)

Set the name of the catalog for this insert.

setColumnNamesback to summary
public void setColumnNames(List<String> columnNames)

Set the names of the columns to be used.

setGeneratedKeyNameback to summary
public void setGeneratedKeyName(String generatedKeyName)

Specify the name of a single generated key column.

setGeneratedKeyNamesback to summary
public void setGeneratedKeyNames(String... generatedKeyNames)

Set the names of any generated keys.

setOverrideIncludeSynonymsDefaultback to summary
public void setOverrideIncludeSynonymsDefault(boolean override)

Specify whether the default for including synonyms should be changed.

The default is false.

setParameterValuesback to summary
private void setParameterValues(PreparedStatement preparedStatement, List<?> values, int... columnTypes) throws SQLException

Internal implementation for setting parameter values.

Parameters
preparedStatement:PreparedStatement

the PreparedStatement

values:List<?>

the values to be set

setQuoteIdentifiersback to summary
public void setQuoteIdentifiers(boolean quoteIdentifiers)

Specify whether SQL identifiers should be quoted.

Defaults to false. If set to true, the identifier quote string for the underlying database will be used to quote SQL identifiers in generated SQL statements.

Parameters
quoteIdentifiers:boolean

whether identifiers should be quoted

Since
6.1
See Also
java.sql.DatabaseMetaData#getIdentifierQuoteString()
setSchemaNameback to summary
public void setSchemaName(String schemaName)

Set the name of the schema for this insert.

setTableNameback to summary
public void setTableName(String tableName)

Set the name of the table for this insert.