Class SequenceStyleGenerator

java.lang.Object
org.hibernate.id.enhanced.SequenceStyleGenerator
All Implemented Interfaces:
Serializable, ExportableProducer, BeforeExecutionGenerator, Generator, BulkInsertionCapableIdentifierGenerator, Configurable, IdentifierGenerator, OptimizableGenerator, PersistentIdentifierGenerator
Direct Known Subclasses:
OrderedSequenceGenerator

Generates identifier values based on a sequence-style database structure. Variations range from actually using a sequence to using a table to mimic a sequence. These variations are encapsulated by the DatabaseStructure interface internally.

General configuration parameters
Parameter name Default value Interpretation
"sequence_name" The name of the sequence/table to use to store/retrieve values
"initial_value" 1 The initial value to be stored for the given segment; the effect in terms of storage varies based on Optimizer and DatabaseStructure
"increment_size" 50 The increment size for the underlying segment; the effect in terms of storage varies based on Optimizer and DatabaseStructure
"optimizer" depends on defined increment size Allows explicit definition of which optimization strategy to use
"force_table_use" false Allows explicit definition of which optimization strategy to use

Configuration parameters used specifically when the underlying structure is a table:

Table configuration parameters
Parameter name Default value Interpretation
"value_column" "next_val" The name of column which holds the sequence value for the given segment
See Also:
  • Field Details

  • Constructor Details

    • SequenceStyleGenerator

      public SequenceStyleGenerator()
  • Method Details

    • getDatabaseStructure

      public DatabaseStructure getDatabaseStructure()
      Getter for property 'databaseStructure'.
      Returns:
      Value for property 'databaseStructure'.
    • getOptimizer

      public Optimizer getOptimizer()
      Getter for property 'optimizer'.
      Specified by:
      getOptimizer in interface OptimizableGenerator
      Returns:
      Value for property 'optimizer'.
    • getIdentifierType

      public Type getIdentifierType()
      Getter for property 'identifierType'.
      Returns:
      Value for property 'identifierType'.
    • configure

      public void configure(GeneratorCreationContext creationContext, Properties parameters) throws MappingException
      Description copied from interface: Configurable
      Configure this instance, given the value of parameters specified by the user as XML <param> elements and @Parameter annotations.

      This method is called just once, following instantiation. If this instance also implements ExportableProducer, then this method is always called before ExportableProducer.registerExportables(Database),

      Specified by:
      configure in interface Configurable
      Parameters:
      creationContext - Access to the generator creation context
      parameters - param values, keyed by parameter name
      Throws:
      MappingException - when there's something wrong with the given parameters
    • 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 Configurable.configure(GeneratorCreationContext, Properties).

      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
    • determineSequenceName

      protected QualifiedName determineSequenceName(Properties params, JdbcEnvironment jdbcEnv, ServiceRegistry serviceRegistry)
      Determine the name of the sequence (or table if this resolves to a physical table) to use.

      Called during configuration.

      Parameters:
      params - The params supplied in the generator config (plus some standard useful extras).
      jdbcEnv - The JdbcEnvironment
      Returns:
      The sequence name
    • determineValueColumnName

      protected Identifier determineValueColumnName(Properties params, JdbcEnvironment jdbcEnvironment)
      Determine the name of the column used to store the generator value in the db.

      Called during configuration when resolving to a physical table.

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

      protected int determineInitialValue(Properties params)
      Determine the initial sequence value to use. This value is used when initializing the database structure (i.e. sequence/table).

      Called during configuration.

      Parameters:
      params - The params supplied in the generator config (plus some standard useful extras).
      Returns:
      The initial value
    • determineIncrementSize

      protected int determineIncrementSize(Properties params)
      Determine the increment size to be applied. The exact implications of this value depends on the optimizer being used.

      Called during configuration.

      Parameters:
      params - The params supplied in the generator config (plus some standard useful extras).
      Returns:
      The increment size
    • determineOptimizationStrategy

      protected OptimizerDescriptor determineOptimizationStrategy(Properties params, int incrementSize)
      Determine the optimizer to use.

      Called during configuration.

      Parameters:
      params - The params supplied in the generator config (plus some standard useful extras).
      incrementSize - The determined increment size
      Returns:
      The optimizer strategy (name)
    • determineAdjustedIncrementSize

      protected int determineAdjustedIncrementSize(OptimizerDescriptor optimizationStrategy, int incrementSize)
      In certain cases we need to adjust the increment size based on the selected optimizer. This is the hook to achieve that.
      Parameters:
      optimizationStrategy - The optimizer strategy (name)
      incrementSize - The determined increment size
      Returns:
      The adjusted increment size.
    • buildDatabaseStructure

      protected DatabaseStructure buildDatabaseStructure(Type type, Properties params, JdbcEnvironment jdbcEnvironment, boolean forceTableUse, QualifiedName sequenceName, int initialValue, int incrementSize)
      Build the database structure.
      Parameters:
      type - The Hibernate type of the identifier property
      params - The params supplied in the generator config (plus some standard useful extras).
      jdbcEnvironment - The JDBC environment in which the sequence will be used.
      forceTableUse - Should a table be used even if the dialect supports sequences?
      sequenceName - The name to use for the sequence or table.
      initialValue - The initial value.
      incrementSize - the increment size to use (after any adjustments).
      Returns:
      An abstraction for the actual database structure in use (table vs. sequence).
    • isPhysicalSequence

      protected boolean isPhysicalSequence(JdbcEnvironment jdbcEnvironment, boolean forceTableUse)
    • buildSequenceStructure

      protected DatabaseStructure buildSequenceStructure(Type type, Properties params, JdbcEnvironment jdbcEnvironment, QualifiedName sequenceName, int initialValue, int incrementSize)
    • buildTableStructure

      protected DatabaseStructure buildTableStructure(Type type, Properties params, JdbcEnvironment jdbcEnvironment, QualifiedName sequenceName, int initialValue, int incrementSize)
    • generate

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

      public boolean supportsBulkInsertionIdentifierGeneration()
      Description copied from interface: BulkInsertionCapableIdentifierGenerator
      Given the configuration of this generator, is identifier generation as part of bulk insertion supported?
      Specified by:
      supportsBulkInsertionIdentifierGeneration in interface BulkInsertionCapableIdentifierGenerator
      Returns:
      true if bulk insertions are supported; false otherwise.
    • determineBulkInsertionIdentifierGenerationSelectFragment

      public String determineBulkInsertionIdentifierGenerationSelectFragment(SqlStringGenerationContext context)
      Description copied from interface: BulkInsertionCapableIdentifierGenerator
      Return the select expression fragment, if any, that generates the identifier values.
      Specified by:
      determineBulkInsertionIdentifierGenerationSelectFragment in interface BulkInsertionCapableIdentifierGenerator
      Returns:
      The identifier value generation fragment (SQL). null indicates that no fragment is needed.
    • applyConfiguration

      public static void applyConfiguration(SequenceGenerator generatorConfig, BiConsumer<String,String> configCollector)