R
- class type which contains members for columns of a row in a tablepublic class InsertOperation<R> extends ModifyOperation<R>
Constructor and Description |
---|
InsertOperation(Table<R> table)
Constructs for standard sql insert statement as:
INSERT INTO table (c1, c2, c3, ...) VALUES (?, ?, ?, ...) |
InsertOperation(Table<R> table,
boolean identity)
Constructs for optional identity column.
|
Modifier and Type | Method and Description |
---|---|
protected void |
initBaseSql()
Sets base sql with
SqlOperation.setBaseSql(String) . |
int |
insert(java.lang.Object... parameters)
Inserts rows based upon parameters.
|
int |
insert(R row)
Inserts a row.
|
int |
insertAll(java.util.Collection<R> rows)
Inserts all rows in collection.
|
boolean |
isAutoGeneratedKeys()
If true,
Statement.getGeneratedKeys() is used to read column defined as
identity and then sets the generated key value in the row. |
void |
notifyCacheModified(R row)
Notifies cache that row has been inserted into database.
|
protected boolean |
notifyCacheModify(R row)
Tests if row is managed by cache.
|
protected void |
postExecute(R row)
Updates identity column with generated key if
isAutoGeneratedKeys() is true. |
protected java.util.List<CascadeOperation<R,?>> |
prepareCascades(java.lang.reflect.Field field)
Creates and prepares cascade operations based upon cascade annotations for a field.
|
protected java.sql.PreparedStatement |
prepareStatement()
Creates the prepared statement.
|
protected void |
processIdentityColumn(R row)
Reads the generated key using
Statement.getGeneratedKeys() . |
void |
setAutoGeneratedKeys(boolean autoGeneratedKeys)
Sets when to get generated keys from database.
|
void |
setBatch(boolean batch)
Sets batch mode.
|
close, execute, getModifyCounts, getRows, getRowsAffected, isBatch, modify, modify, modifyAll, postExecuteCascade, preExecute, preExecuteCascade, setParameters, setRow, setRows, setRows, setRows, setRowsAffected
cancel, cascade, closeCascades, closeStatement, createRowField, getBaseSql, getCascadeDepth, getConnection, getCustomSql, getNamedParameterMap, getNextParameter, getOperationTime, getParameter, getParameters, getPreparedSql, getPreparedStatement, getQueryTimeout, getRequiredCascades, getSql, getTable, getTargetTable, getTimingId, getWhereAnnotation, getWhereConditionName, getWhereTranslator, initOperationTime, isCached, isCascade, isCascading, isIncludeIdentityColumns, isPrimaryKey, isReadOnly, isRequiredCascade, isTimings, logTimings, prepare, prepareCascades, prepareCheck, setBaseSql, setCached, setCascade, setCascadeDepth, setCustomSql, setIncludeIdentityColumns, setNamedParameterMap, setNextParameter, setParameter, setQueryTimeout, setReadOnly, setRequiredCascades, setTimingId, setTimings, setWhere, setWhereTranslator, writeColumns, writeParameter, writeParameters, writeWhere
public InsertOperation(Table<R> table) throws OperationException
table
- insert into this tableOperationException
- if errorpublic InsertOperation(Table<R> table, boolean identity) throws OperationException
setAutoGeneratedKeys(boolean)
.
To insert a row with an identity column defined but without generating keys, set
identity parameter to false.table
- insert into this tableidentity
- true to use autogenerated keys for identity columns;
false to insert identity columns as without generating keyOperationException
- if errorpublic boolean isAutoGeneratedKeys()
Statement.getGeneratedKeys()
is used to read column defined as
identity and then sets the generated key value in the row. Default is false. Typically
true for insert operations.processIdentityColumn(Object)
public void setAutoGeneratedKeys(boolean autoGeneratedKeys)
autoGeneratedKeys
- true if identity column is generated by databasepublic void setBatch(boolean batch)
ModifyOperation.execute()
to insert/update/delete rows using
JDBC batch mode. Batch mode does not support identity columns. Auto commit must
be off for batch mode. ModifyOperation.preExecute(Object)
is invoked prior to batch add and
ModifyOperation.postExecute(Object)
is invoked after batch execute.
Since version 4.1, cascades are performed for batch mode.
Batch modifications are not cached. So when batch is true and table is cached, then
table cache is flushed prior to executing batch modifications to avoid inconsistencies in
cache. Database.flush()
may be required if batched rows affect foreign key
relationships.
Logs error if batch and isAutoGeneratedKeys()
are both true.
setBatch
in class ModifyOperation<R>
batch
- true to use JDBC batching for ModifyOperation.execute()
Statement.executeBatch()
public int insert(R row) throws OperationException
ModifyOperation.modify(Object)
.row
- row to use for parametersModifyOperation.getRowsAffected()
OperationException
- if errorpublic int insertAll(java.util.Collection<R> rows) throws OperationException
ModifyOperation.modifyAll(Collection)
.rows
- collection of rows to use as parametersModifyOperation.getRowsAffected()
OperationException
- if errorpublic int insert(java.lang.Object... parameters) throws OperationException
ModifyOperation.modify(Object...)
.parameters
- operation parameters as objects (see ModifyOperation.setParameters(Object...)
)OperationException
- if errorprotected java.sql.PreparedStatement prepareStatement() throws java.sql.SQLException
prepareStatement
in class SqlOperation<R>
java.sql.SQLException
- if errorprotected void initBaseSql()
SqlOperation.setBaseSql(String)
.protected java.util.List<CascadeOperation<R,?>> prepareCascades(java.lang.reflect.Field field) throws OperationException
prepareCascades
in class SqlOperation<R>
field
- annotation is for this field of row class ROperationException
- if errorprotected void postExecute(R row) throws OperationException
isAutoGeneratedKeys()
is true.postExecute
in class ModifyOperation<R>
row
- row to affectOperationException
- if errorprotected void processIdentityColumn(R row) throws OperationException
Statement.getGeneratedKeys()
. The
generated key is set in row using the ColumnTranslator
from the table for
this operation as RowTranslator.getIdentityColumnTranslator()
.row
- row to affectOperationException
- if errorprotected boolean notifyCacheModify(R row) throws OperationException
Cache.insert(Object)
.notifyCacheModify
in class ModifyOperation<R>
row
- test this rowOperationException
- if cache reports an errorpublic void notifyCacheModified(R row) throws OperationException
Cache.inserted(Object)
.notifyCacheModified
in class ModifyOperation<R>
row
- row that was insertedOperationException
- if cache reports an error