Class GeneratedIdPropertyType<ENTITY,BASIC extends Number,CONTAINER>

java.lang.Object
org.seasar.doma.jdbc.entity.DefaultPropertyType<ENTITY,BASIC,CONTAINER>
org.seasar.doma.jdbc.entity.GeneratedIdPropertyType<ENTITY,BASIC,CONTAINER>
Type Parameters:
ENTITY - the entity type that contains this property
BASIC - the numeric type of the ID property (must extend Number)
CONTAINER - the container type that holds the property value (e.g., Optional)
All Implemented Interfaces:
EntityPropertyType<ENTITY,BASIC>

public class GeneratedIdPropertyType<ENTITY,BASIC extends Number,CONTAINER> extends DefaultPropertyType<ENTITY,BASIC,CONTAINER>
A property type for entity ID fields whose values are automatically generated.

This class handles ID generation strategies such as database identity columns, sequences, and table-based ID generation. It provides methods for generating IDs both before and after database insert operations.

This property type is used for fields annotated with both Id and GeneratedValue in entity classes.

See Also:
  • Field Details

    • idGenerator

      protected final IdGenerator idGenerator
  • Constructor Details

    • GeneratedIdPropertyType

      public GeneratedIdPropertyType(Class<ENTITY> entityClass, Supplier<Scalar<BASIC,CONTAINER>> scalarSupplier, String name, String columnName, NamingType namingType, boolean quoteRequired, IdGenerator idGenerator)
      Constructs a new generated ID property type.

      This constructor is typically called by the Doma annotation processor when generating implementations of EntityType.

      Parameters:
      entityClass - the entity class
      scalarSupplier - the supplier of scalar that represents the property value
      name - the property name
      columnName - the column name
      namingType - the naming convention
      quoteRequired - whether the column name requires quoting in SQL
      idGenerator - the ID generator for this property
      Throws:
      DomaNullPointerException - if idGenerator is null
  • Method Details

    • isId

      public boolean isId()
      Determines whether this property represents a primary key (identifier).

      This method indicates if the property is annotated with Id, which marks it as a primary key column in the database table.

      This implementation always returns true since this property type represents an ID property with a generated value.

      Specified by:
      isId in interface EntityPropertyType<ENTITY,BASIC extends Number>
      Overrides:
      isId in class DefaultPropertyType<ENTITY,BASIC extends Number,CONTAINER>
      Returns:
      true
      See Also:
    • validateGenerationStrategy

      public void validateGenerationStrategy(IdGenerationConfig config)
      Validates that the ID generation strategy is supported by the current database dialect.

      This method checks if the configured generation strategy (IDENTITY, SEQUENCE, etc.) is compatible with the database dialect being used. If not, a JdbcException is thrown.

      Parameters:
      config - the ID generation configuration
      Throws:
      JdbcException - if the generation strategy is not supported by the dialect
    • isGenerationTypeSupported

      protected boolean isGenerationTypeSupported(GenerationType generationType, Dialect dialect)
      Determines if the specified generation type is supported by the given dialect.

      Different databases support different ID generation strategies. For example, not all databases support identity columns or sequences.

      Parameters:
      generationType - the ID generation type to check
      dialect - the database dialect
      Returns:
      true if the generation type is supported, false otherwise
    • isIncluded

      @Deprecated public boolean isIncluded(IdGenerationConfig config)
      Deprecated.
    • isIncluded

      public boolean isIncluded(IdGenerationConfig config, Object idValue)
    • isBatchSupported

      public boolean isBatchSupported(IdGenerationConfig config)
    • isAutoGeneratedKeysSupported

      public boolean isAutoGeneratedKeysSupported(IdGenerationConfig config)
    • getGenerationType

      public GenerationType getGenerationType()
    • preInsert

      public ENTITY preInsert(EntityType<ENTITY> entityType, ENTITY entity, IdGenerationConfig config)
      Generates and sets an ID value for an entity before it is inserted into the database.

      This method is used for ID generation strategies that generate values before the insert operation, such as SEQUENCE and TABLE.

      Parameters:
      entityType - the entity type
      entity - the entity instance
      config - the ID generation configuration
      Returns:
      the entity with the generated ID set (if necessary)
    • preInsert

      public List<ENTITY> preInsert(EntityType<ENTITY> entityType, List<ENTITY> entities, IdGenerationConfig config)
      Generates and sets ID values for multiple entities before they are inserted into the database.

      This method is used for batch operations with ID generation strategies that generate values before the insert operation, such as SEQUENCE and TABLE.

      Parameters:
      entityType - the entity type
      entities - the list of entity instances
      config - the ID generation configuration
      Returns:
      the list of entities with generated IDs set (if necessary)
    • postInsert

      public ENTITY postInsert(EntityType<ENTITY> entityType, ENTITY entity, IdGenerationConfig config, Statement statement)
      Retrieves and sets an ID value for an entity after it has been inserted into the database.

      This method is used for ID generation strategies that generate values during the insert operation, such as IDENTITY.

      Parameters:
      entityType - the entity type
      entity - the entity instance
      config - the ID generation configuration
      statement - the JDBC statement used for the insert operation
      Returns:
      the entity with the generated ID set (if necessary)
    • postInsert

      public List<ENTITY> postInsert(EntityType<ENTITY> entityType, List<ENTITY> entities, IdGenerationConfig config, Statement statement)
      Retrieves and sets ID values for multiple entities after they have been inserted into the database.

      This method is used for batch operations with ID generation strategies that generate values during the insert operation, such as IDENTITY.

      Parameters:
      entityType - the entity type
      entities - the list of entity instances
      config - the ID generation configuration
      statement - the JDBC statement used for the insert operation
      Returns:
      the list of entities with generated IDs set (if necessary)
    • setIfNecessary

      protected ENTITY setIfNecessary(EntityType<ENTITY> entityType, ENTITY entity, Supplier<Long> supplier)
      Sets the ID value if necessary.

      This method sets the ID value only if the current value is null or negative. It is used internally by the preInsert and postInsert methods.

      Parameters:
      entityType - the entity type metadata
      entity - the entity instance to modify
      supplier - the supplier of the ID value to set
      Returns:
      the modified entity instance (may be the same instance if the entity is mutable)