- Type Parameters:
ENTITY- the entity type
- All Implemented Interfaces:
InsertQuery,ModifyQuery,Query
This class provides functionality to generate and execute SQL INSERT statements based on entity definitions. It handles various insert scenarios including:
- Standard inserts with all entity properties
- Inserts with null property exclusion
- Inserts with generated ID values (identity or sequence-based)
- Inserts with version number initialization
- Handling of duplicate key scenarios (exception or update)
The query execution process includes:
- Pre-insert entity processing
- SQL statement preparation
- Statement execution
- Generated ID retrieval (if applicable)
- Post-insert entity processing
-
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.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.protected booleanIndicates whether null properties should be excluded from the INSERT statement.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 an ID for the inserted entity.protected voidExecutes post-insert entity processing.protected voidExecutes pre-insert entity processing.voidprepare()Prepares this query for execution.protected voidPrepares the ID value for the entity 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 value for the entity 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.voidsetNullExcluded(boolean nullExcluded) Sets whether null properties should be excluded from the INSERT statement.Methods inherited from class org.seasar.doma.jdbc.query.AutoModifyQuery
getEntity, getSql, getSqlExecutionSkipCause, getSqlLogType, isAutoGeneratedKeysSupported, isExecutable, isOptimisticLockCheckRequired, isTargetPropertyName, prepareOptions, setEntity, 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
-
nullExcluded
protected boolean nullExcludedIndicates whether null properties should be excluded from the INSERT statement. -
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
-
AutoInsertQuery
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
- Executes pre-insert entity processing
- 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.This method creates a pre-insert context and calls the entity's preInsert method, allowing entity listeners to modify the entity 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.
- 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
- If nullExcluded is true, null-valued properties are excluded
- 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 value for the entity before insertion.If the entity has a generated ID property, this method calls its preInsert method to generate or prepare the ID value before the INSERT operation.
-
prepareVersionValue
protected void prepareVersionValue()Prepares the version value for the entity before insertion.If the entity has 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 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 an ID for the inserted entity.This method is called after executing the INSERT statement to retrieve and set auto-generated keys for the entity. 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.This method creates a post-insert context and calls the entity's postInsert method, allowing entity listeners to modify the entity after insertion.
-
setNullExcluded
public void setNullExcluded(boolean nullExcluded) Sets whether null properties should be excluded from the INSERT statement.- Parameters:
nullExcluded- true to exclude null properties, false otherwise
-
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
-