- Type Parameters:
ENTITY- the entity type
- All Implemented Interfaces:
InsertQuery,ModifyQuery,Query
This class provides functionality to generate and execute SQL multi-row INSERT statements based on entity definitions. It handles various insert scenarios including:
- Batch inserts with multiple entities in a single statement
- Inserts with generated ID values (sequence-based only for multiple rows)
- Inserts with version number initialization
- Handling of duplicate key scenarios (exception or update)
The query execution process includes:
- Pre-insert entity processing for each entity
- SQL statement preparation for multiple rows
- Statement execution
- Generated ID retrieval (if applicable)
- Post-insert entity processing for each entity
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static classA context class for post-insert entity processing.protected static classA context class for pre-insert entity processing. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected String[]The names of properties that form the unique key for duplicate key handling.protected DuplicateKeyTypeThe strategy for handling duplicate key violations.The list of entities to be inserted.protected GeneratedIdPropertyType<ENTITY,?, ?> The property type for the generated ID, if the entity has one.protected IdGenerationConfigConfiguration for ID generation, used when the entity has a generated ID property.Fields inherited from class org.seasar.doma.jdbc.query.AutoModifyQuery
autoGeneratedKeysSupported, EMPTY_STRINGS, entity, entityType, excludedPropertyNames, executable, idPropertyTypes, includedPropertyNames, optimisticLockCheckRequired, returning, sql, sqlExecutionSkipCause, sqlLogType, targetPropertyTypes, tenantIdPropertyType, versionPropertyTypeFields inherited from class org.seasar.doma.jdbc.query.AbstractQuery
callerClassName, callerMethodName, config, message, method, queryTimeout -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidcomplete()Completes this query by executing post-insert processing.voidgenerateId(Statement statement) Generates IDs for the inserted entities.Returns the list of entities to be inserted.This method is not supported for multi-insert queries.protected voidExecutes post-insert entity processing for each entity.protected voidExecutes pre-insert entity processing for each entity.voidprepare()Prepares this query for execution.protected voidPrepares the ID values for the entities before insertion.protected voidPrepares special property types for this query.protected voidPrepares the SQL statement for this query.protected voidPrepares the target property types for the INSERT statement.protected voidPrepares the version values for the entities before insertion.voidsetDuplicateKeyNames(String... duplicateKeyNames) Sets the names of properties that form the unique key for duplicate key handling.voidsetDuplicateKeyType(DuplicateKeyType duplicateKeyType) Sets the strategy for handling duplicate key violations.voidsetEntities(List<ENTITY> entities) Sets the list of entities to be inserted.voidThis method is not supported for multi-insert queries.Methods inherited from class org.seasar.doma.jdbc.query.AutoModifyQuery
getSql, getSqlExecutionSkipCause, getSqlLogType, isAutoGeneratedKeysSupported, isExecutable, isOptimisticLockCheckRequired, isTargetPropertyName, prepareOptions, setExcludedPropertyNames, setIncludedPropertyNames, setReturning, setSqlLogType, toString, validateIdExistentMethods inherited from class org.seasar.doma.jdbc.query.AbstractQuery
comment, getClassName, getConfig, getMethod, getMethodName, getQueryTimeout, setCallerClassName, setCallerMethodName, setConfig, setMessage, setMethod, setQueryTimeoutMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.seasar.doma.jdbc.query.ModifyQuery
getSql, getSqlExecutionSkipCause, getSqlLogType, isAutoGeneratedKeysSupported, isExecutable, isOptimisticLockCheckRequiredMethods inherited from interface org.seasar.doma.jdbc.query.Query
comment, getClassName, getConfig, getMethod, getMethodName, getQueryTimeout
-
Field Details
-
entities
The list of entities to be inserted. -
generatedIdPropertyType
The property type for the generated ID, if the entity has one. -
idGenerationConfig
Configuration for ID generation, used when the entity has a generated ID property. -
duplicateKeyType
The strategy for handling duplicate key violations. Default isDuplicateKeyType.EXCEPTION, which throws an exception on duplicate key. -
duplicateKeyNames
The names of properties that form the unique key for duplicate key handling. Used whenduplicateKeyTypeis notDuplicateKeyType.EXCEPTION.
-
-
Constructor Details
-
AutoMultiInsertQuery
Constructs an instance.- Parameters:
entityType- the entity type
-
-
Method Details
-
prepare
public void prepare()Prepares this query for execution.This method performs the following operations:
- Validates that required components are not null
- Checks if the dialect supports multi-row INSERT statements
- Skips execution if the entity list is empty
- Executes pre-insert entity processing for each entity
- Prepares special property types (ID, version, etc.)
- Prepares query options
- Determines target properties for the INSERT statement
- Prepares ID and version values
- Builds the SQL statement
- Specified by:
preparein interfaceQuery- Overrides:
preparein classAutoModifyQuery<ENTITY>
-
preInsert
protected void preInsert()Executes pre-insert entity processing for each entity.This method creates a pre-insert context for each entity and calls the entity's preInsert method, allowing entity listeners to modify the entities before insertion.
-
prepareSpecialPropertyTypes
protected void prepareSpecialPropertyTypes()Prepares special property types for this query.This method initializes the generated ID property type and its configuration, and determines if auto-generated keys are supported for this query.
It also checks if the dialect supports auto-increment when inserting multiple rows, and throws an exception if not supported with IDENTITY generation type.
- Overrides:
prepareSpecialPropertyTypesin classAutoModifyQuery<ENTITY>
-
prepareTargetPropertyType
protected void prepareTargetPropertyType()Prepares the target property types for the INSERT statement.This method determines which entity properties should be included in the INSERT statement based on the following rules:
- Properties must be insertable
- ID properties are included if they are not auto-generated or if they have a value
- Version properties are always included
- Properties must match the include/exclude name filters if specified
This method throws a JdbcException if a non-generated ID property has a null value.
-
prepareIdValue
protected void prepareIdValue()Prepares the ID values for the entities before insertion.If the entities have a generated ID property, this method calls its preInsert method to generate or prepare the ID values before the INSERT operation.
-
prepareVersionValue
protected void prepareVersionValue()Prepares the version values for the entities before insertion.If the entities have a version property, this method initializes it to 1 for optimistic locking.
-
prepareSql
protected void prepareSql()Prepares the SQL statement for this query.This method builds either a standard multi-row INSERT statement or an UPSERT statement based on the duplicate key handling strategy. It uses the dialect-specific SQL assemblers to generate the appropriate SQL syntax.
If the duplicate key type is EXCEPTION, a standard INSERT statement is generated. Otherwise, an UPSERT statement is generated, unless the dialect supports MERGE statements and an identity key is included in the duplicate keys, in which case it falls back to a standard INSERT.
-
generateId
Generates IDs for the inserted entities.This method is called after executing the INSERT statement to retrieve and set auto-generated keys for the entities. It's only executed if auto-generated keys are supported for this query.
- Specified by:
generateIdin interfaceInsertQuery- Parameters:
statement- the statement used for the INSERT operation
-
complete
public void complete()Completes this query by executing post-insert processing.This method is called after the INSERT statement has been executed and any generated IDs have been retrieved.
-
postInsert
protected void postInsert()Executes post-insert entity processing for each entity.This method creates a post-insert context for each entity and calls the entity's postInsert method, allowing entity listeners to modify the entities after insertion.
-
setDuplicateKeyType
Sets the strategy for handling duplicate key violations.- Parameters:
duplicateKeyType- the duplicate key handling strategy
-
setDuplicateKeyNames
Sets the names of properties that form the unique key for duplicate key handling.- Parameters:
duplicateKeyNames- the property names that form the unique key
-
setEntities
Sets the list of entities to be inserted.- Parameters:
entities- the list of entities
-
getEntities
Returns the list of entities to be inserted.- Returns:
- the list of entities
-
setEntity
This method is not supported for multi-insert queries. UsesetEntities(List)instead.- Overrides:
setEntityin classAutoModifyQuery<ENTITY>- Parameters:
entity- the entity- Throws:
UnsupportedOperationException- always thrown
-
getEntity
This method is not supported for multi-insert queries. UsegetEntities()instead.- Overrides:
getEntityin classAutoModifyQuery<ENTITY>- Returns:
- never returns
- Throws:
UnsupportedOperationException- always thrown
-