Class AutoBatchInsertQuery<ENTITY>

Type Parameters:
ENTITY - the entity type
All Implemented Interfaces:
BatchInsertQuery, BatchModifyQuery, Query

public class AutoBatchInsertQuery<ENTITY> extends AutoBatchModifyQuery<ENTITY> implements BatchInsertQuery
An auto batch insert query that is executed against an entity class.

This class implements BatchInsertQuery to provide batch INSERT operations for entities. It handles ID generation, version initialization, and supports various duplicate key handling strategies.

  • Field Details

    • generatedIdPropertyType

      protected GeneratedIdPropertyType<ENTITY,?,?> generatedIdPropertyType
      The property type for the generated ID.
    • idGenerationConfig

      protected IdGenerationConfig idGenerationConfig
      The configuration for ID generation.
    • batchSupported

      protected boolean batchSupported
      Whether batch operations are supported.
    • generatedKeysIgnored

      protected boolean generatedKeysIgnored
      Whether to ignore auto-generated keys.
    • duplicateKeyType

      protected DuplicateKeyType duplicateKeyType
      The strategy for handling duplicate keys.
    • duplicateKeyNames

      protected String[] duplicateKeyNames
      The names of columns that may cause duplicate key violations.
  • Constructor Details

    • AutoBatchInsertQuery

      public AutoBatchInsertQuery(EntityType<ENTITY> entityType)
      Constructs an instance.
      Parameters:
      entityType - the entity type
  • Method Details

    • setGeneratedKeysIgnored

      public void setGeneratedKeysIgnored(boolean generatedKeysIgnored)
      Sets whether to ignore auto-generated keys.
      Parameters:
      generatedKeysIgnored - whether to ignore auto-generated keys
    • setDuplicateKeyType

      public void setDuplicateKeyType(DuplicateKeyType duplicateKeyType)
      Sets the strategy for handling duplicate keys.
      Parameters:
      duplicateKeyType - the strategy for handling duplicate keys
    • setDuplicateKeyNames

      public void setDuplicateKeyNames(String... duplicateKeyNames)
      Sets the names of columns that may cause duplicate key violations.
      Parameters:
      duplicateKeyNames - the names of columns that may cause duplicate key violations
    • prepare

      public void prepare()
      Prepares this query for execution.

      This method processes all entities in the batch, applying pre-insert hooks, preparing ID and version values, and generating SQL statements for each entity.

      Specified by:
      prepare in interface Query
      Overrides:
      prepare in class AbstractQuery
    • preInsert

      protected void preInsert()
      Executes pre-insert hooks on the current entity.

      This method creates a context for pre-insert processing and applies entity-specific pre-insert logic to the current entity.

    • prepareIdAndVersionPropertyTypes

      protected void prepareIdAndVersionPropertyTypes()
      Prepares ID and version property types for this query.

      This method initializes the generated ID property type and configures ID generation settings, including whether batch operations and auto-generated keys are supported.

      Overrides:
      prepareIdAndVersionPropertyTypes in class AutoBatchModifyQuery<ENTITY>
    • prepareTargetPropertyTypes

      protected void prepareTargetPropertyTypes()
      Prepares the target property types for this query.

      This method determines which entity properties should be included in the INSERT statement based on their insertability, ID status, and other criteria. It also validates that non-generated ID properties have non-null values.

    • prepareIdValue

      protected void prepareIdValue()
      Prepares the ID value for the current entity.

      If a generated ID property type is configured, this method applies pre-insert ID generation logic to the current entity.

    • prepareVersionValue

      protected void prepareVersionValue()
      Prepares the version value for the current entity.

      If a version property type is configured, this method initializes the version value to 1 for the current entity.

    • prepareSql

      protected void prepareSql()
      Prepares the SQL statement for the current entity.

      This method builds the appropriate SQL statement (INSERT or UPSERT) based on the duplicate key handling strategy and dialect capabilities.

    • isBatchSupported

      public boolean isBatchSupported()
      Determines whether batch operations are supported for this query.
      Specified by:
      isBatchSupported in interface BatchInsertQuery
      Returns:
      true if batch operations are supported, false otherwise
    • generateId

      public void generateId(Statement statement, int index)
      Generates an ID for the entity at the specified index using auto-generated keys.

      This method retrieves the auto-generated key from the statement and updates the entity with the generated ID value.

      Specified by:
      generateId in interface BatchInsertQuery
      Parameters:
      statement - the statement that executed the insert
      index - the index of the entity in the batch
    • generateIds

      public void generateIds(Statement statement, int position, int size)
      Generates IDs for a range of entities using auto-generated keys.

      This method retrieves auto-generated keys from the statement and updates the specified range of entities with the generated ID values.

      Specified by:
      generateIds in interface BatchInsertQuery
      Parameters:
      statement - the statement that executed the batch insert
      position - the starting position in the entities list
      size - the number of entities to process
      Throws:
      DomaIllegalArgumentException - if the position and size parameters are invalid
    • complete

      public void complete()
      Completes the batch insert operation.

      This method applies post-insert hooks to all entities in the batch after the database operation has been executed.

      Specified by:
      complete in interface Query
    • postInsert

      protected void postInsert()
      Executes post-insert hooks on the current entity.

      This method creates a context for post-insert processing and applies entity-specific post-insert logic to the current entity.