Class TableGenerator

java.lang.Object
org.hibernate.id.enhanced.TableGenerator
All Implemented Interfaces:
Serializable, ExportableProducer, BeforeExecutionGenerator, Generator, Configurable, StandardGenerator, IdentifierGenerator, OptimizableGenerator, PersistentIdentifierGenerator

public class TableGenerator extends Object implements PersistentIdentifierGenerator
An enhanced version of table-based id generation.

Unlike the simplistic legacy one (which was only ever intended for subclassing support) we "segment" the table into multiple values. Thus, a single table can actually serve as the persistent storage for multiple independent generators. One approach would be to segment the values by the name of the entity for which we are performing generation, which would mean that we would have a row in the generator table for each entity name. Or any configuration really; the setup is very flexible.

By default, we use a single row for all generators (the "default" segment). The configuration parameter "prefer_entity_table_as_segment_value" can be used to change that to instead default to using a row for each entity name.

Configuration parameters
Parameter name Default value Interpretation
"table_name" "hibernate_sequences" The name of the table to use to store/retrieve values
"value_column_name" "next_val" The name of column which holds the sequence value for the given segment
"segment_column_name" "sequence_name" The name of the column which holds the segment key
"segment_value" "default" The value indicating which segment is used by this generator; refers to values in the "segment_column_name" column
"segment_value_length" 255 The data length of the "segment_column_name" column; used for schema creation
"initial_value" 1 The initial value to be stored for the given segment
"increment_size" 50 The increment size for the underlying segment; see the discussion on Optimizer for more details.
"optimizer" depends on defined increment size Allows explicit definition of which optimization strategy to use
See Also:
  • Field Details

  • Constructor Details

    • TableGenerator

      public TableGenerator()
  • Method Details

    • getIdentifierType

      public final Type getIdentifierType()
      Type mapping for the identifier.
      Returns:
      The identifier type mapping.
    • getTableName

      public final String getTableName()
      The name of the table in which we store this generator's persistent state.
      Returns:
      The table name.
    • getSegmentColumnName

      public final String getSegmentColumnName()
      The name of the column in which we store the segment to which each row belongs. The value here acts as PK.
      Returns:
      The segment column name
    • getSegmentValue

      public final String getSegmentValue()
      The value in segment column corresponding to this generator instance. In other words this value indicates the row in which this generator instance will store values.
      Returns:
      The segment value for this generator instance.
    • getSegmentValueLength

      public final int getSegmentValueLength()
      The size of the segment column in the underlying table.
      Returns:
      the column size.
      API Note:
      This should really have been called segmentColumnLength or even better segmentColumnSize.
    • getValueColumnName

      public final String getValueColumnName()
      The name of the column in which we store our persistent generator value.
      Returns:
      The name of the value column.
    • getInitialValue

      public final int getInitialValue()
      The initial value to use when we find no previous state in the generator table corresponding to our sequence.
      Returns:
      The initial value to use.
    • getIncrementSize

      public final int getIncrementSize()
      The amount of increment to use. The exact implications of this depends on the optimizer being used.
      Returns:
      The increment amount.
    • getOptimizer

      public final Optimizer getOptimizer()
      The optimizer being used by this generator.
      Specified by:
      getOptimizer in interface OptimizableGenerator
      Returns:
      Out optimizer.
    • getTableAccessCount

      public final long getTableAccessCount()
      Getter for property 'tableAccessCount'. Only really useful for unit test assertions.
      Returns:
      Value for property 'tableAccessCount'.
    • getAllSqlForTests

      @Deprecated public String[] getAllSqlForTests()
      Deprecated.
      Exposed for tests only.
    • configure

      public void configure(Type type, Properties parameters, ServiceRegistry serviceRegistry) throws MappingException
      Description copied from interface: IdentifierGenerator
      Configure this instance, given the value of parameters specified by the user as <param> elements.

      This method is called just once, following instantiation, and before IdentifierGenerator.registerExportables(Database).

      Specified by:
      configure in interface Configurable
      Specified by:
      configure in interface IdentifierGenerator
      Parameters:
      type - The id property type descriptor
      parameters - param values, keyed by parameter name
      serviceRegistry - Access to service that may be needed.
      Throws:
      MappingException - If configuration fails.
    • determineGeneratorTableName

      protected QualifiedName determineGeneratorTableName(Properties params, JdbcEnvironment jdbcEnvironment, ServiceRegistry serviceRegistry)
      Determine the table name to use for the generator values.

      Called during configuration.

      Parameters:
      params - The params supplied in the generator config (plus some standard useful extras).
      jdbcEnvironment - The JDBC environment
      Returns:
      The table name to use.
      See Also:
    • determineSegmentColumnName

      protected String determineSegmentColumnName(Properties params, JdbcEnvironment jdbcEnvironment)
      Determine the name of the column used to indicate the segment for each row. This column acts as the primary key.

      Called during configuration.

      Parameters:
      params - The params supplied in the generator config (plus some standard useful extras).
      jdbcEnvironment - The JDBC environment
      Returns:
      The name of the segment column
      See Also:
    • determineValueColumnName

      protected String determineValueColumnName(Properties params, JdbcEnvironment jdbcEnvironment)
      Determine the name of the column in which we will store the generator persistent value.

      Called during configuration.

      Parameters:
      params - The params supplied in the generator config (plus some standard useful extras).
      jdbcEnvironment - The JDBC environment
      Returns:
      The name of the value column
      See Also:
    • determineSegmentValue

      protected String determineSegmentValue(Properties params)
      Determine the segment value corresponding to this generator instance.

      Called during configuration.

      Parameters:
      params - The params supplied in the generator config (plus some standard useful extras).
      Returns:
      The name of the value column
      See Also:
    • determineDefaultSegmentValue

      protected String determineDefaultSegmentValue(Properties params)
      Used in the cases where determineSegmentValue(java.util.Properties) is unable to determine the value to use.
      Parameters:
      params - The params supplied in the generator config (plus some standard useful extras).
      Returns:
      The default segment value to use.
    • determineSegmentColumnSize

      protected int determineSegmentColumnSize(Properties params)
      Determine the size of the segment column

      Called during configuration.

      Parameters:
      params - The params supplied in the generator config (plus some standard useful extras).
      Returns:
      The size of the segment column
      See Also:
    • determineInitialValue

      protected int determineInitialValue(Properties params)
    • determineIncrementSize

      protected int determineIncrementSize(Properties params)
    • buildSelectQuery

      protected String buildSelectQuery(String formattedPhysicalTableName, SqlStringGenerationContext context)
    • buildUpdateQuery

      protected String buildUpdateQuery(String formattedPhysicalTableName, SqlStringGenerationContext context)
    • buildInsertQuery

      protected String buildInsertQuery(String formattedPhysicalTableName, SqlStringGenerationContext context)
    • generateInsertInitCommand

      protected InitCommand generateInsertInitCommand(SqlStringGenerationContext context)
    • generate

      public Object generate(SharedSessionContractImplementor session, Object obj)
      Description copied from interface: IdentifierGenerator
      Generate a new identifier.
      Specified by:
      generate in interface IdentifierGenerator
      Parameters:
      session - The session from which the request originates
      obj - the entity or collection (idbag) for which the id is being generated
      Returns:
      a new identifier
    • registerExportables

      public void registerExportables(Database database)
      Description copied from interface: IdentifierGenerator
      Register database objects used by this identifier generator, for example, a sequence or tables.

      This method is called just once, after IdentifierGenerator.configure(Type, Properties, ServiceRegistry).

      Specified by:
      registerExportables in interface ExportableProducer
      Specified by:
      registerExportables in interface IdentifierGenerator
      Parameters:
      database - The database instance
    • initialize

      public void initialize(SqlStringGenerationContext context)
      Description copied from interface: Configurable
      Initializes this instance, pre-generating SQL if necessary.

      If this instance also implements ExportableProducer, then this method is always called after ExportableProducer.registerExportables(Database), and before first use.

      Specified by:
      initialize in interface Configurable
      Parameters:
      context - A context to help generate SQL strings