Package org.hibernate.id.enhanced
Class SequenceStyleGenerator
- java.lang.Object
-
- org.hibernate.id.enhanced.SequenceStyleGenerator
-
- All Implemented Interfaces:
Serializable
,ExportableProducer
,BeforeExecutionGenerator
,Generator
,BulkInsertionCapableIdentifierGenerator
,Configurable
,StandardGenerator
,IdentifierGenerator
,OptimizableGenerator
,PersistentIdentifierGenerator
- Direct Known Subclasses:
OrderedSequenceGenerator
public class SequenceStyleGenerator extends Object implements PersistentIdentifierGenerator, BulkInsertionCapableIdentifierGenerator
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 theDatabaseStructure
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
andDatabaseStructure
"increment_size" 50 The increment size for the underlying segment; the effect in terms of storage varies based on Optimizer
andDatabaseStructure
"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:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static String
ALT_SEQUENCE_PARAM
static String
CONFIG_SEQUENCE_PER_ENTITY_SUFFIX
Specifies the suffix to use for an implicit sequence name - appended to the entity-name / collection-rolestatic String
DEF_SEQUENCE_SUFFIX
The default value forCONFIG_SEQUENCE_PER_ENTITY_SUFFIX
static String
DEF_VALUE_COLUMN
The default value forVALUE_COLUMN_PARAM
static String
FORCE_TBL_PARAM
A flag to force using a table as the underlying structure rather than a sequence.static String
SEQUENCE_PARAM
Indicates the name of the sequence (or table) to use.static String
VALUE_COLUMN_PARAM
Indicates the name of the column holding the identifier values.-
Fields inherited from interface org.hibernate.id.IdentifierGenerator
CONTRIBUTOR_NAME, ENTITY_NAME, GENERATOR_NAME, JPA_ENTITY_NAME
-
Fields inherited from interface org.hibernate.id.OptimizableGenerator
DEFAULT_INCREMENT_SIZE, DEFAULT_INITIAL_VALUE, IMPLICIT_NAME_BASE, INCREMENT_PARAM, INITIAL_PARAM, OPT_PARAM
-
Fields inherited from interface org.hibernate.id.PersistentIdentifierGenerator
CATALOG, IDENTIFIER_NORMALIZER, PK, SCHEMA, TABLE, TABLES
-
-
Constructor Summary
Constructors Constructor Description SequenceStyleGenerator()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected DatabaseStructure
buildDatabaseStructure(Type type, Properties params, JdbcEnvironment jdbcEnvironment, boolean forceTableUse, QualifiedName sequenceName, int initialValue, int incrementSize)
Build the database structure.protected DatabaseStructure
buildSequenceStructure(Type type, Properties params, JdbcEnvironment jdbcEnvironment, QualifiedName sequenceName, int initialValue, int incrementSize)
protected DatabaseStructure
buildTableStructure(Type type, Properties params, JdbcEnvironment jdbcEnvironment, QualifiedName sequenceName, int initialValue, int incrementSize)
void
configure(Type type, Properties parameters, ServiceRegistry serviceRegistry)
Configure this instance, given the value of parameters specified by the user as<param>
elements.protected int
determineAdjustedIncrementSize(String optimizationStrategy, int incrementSize)
In certain cases we need to adjust the increment size based on the selected optimizer.String
determineBulkInsertionIdentifierGenerationSelectFragment(SqlStringGenerationContext context)
Return the select expression fragment, if any, that generates the identifier values.protected int
determineIncrementSize(Properties params)
Determine the increment size to be applied.protected int
determineInitialValue(Properties params)
Determine the initial sequence value to use.protected String
determineOptimizationStrategy(Properties params, int incrementSize)
Determine the optimizer to use.protected QualifiedName
determineSequenceName(Properties params, Dialect dialect, JdbcEnvironment jdbcEnv, ServiceRegistry serviceRegistry)
Determine the name of the sequence (or table if this resolves to a physical table) to use.protected Identifier
determineValueColumnName(Properties params, JdbcEnvironment jdbcEnvironment)
Determine the name of the column used to store the generator value in the db.Object
generate(SharedSessionContractImplementor session, Object object)
Generate a new identifier.DatabaseStructure
getDatabaseStructure()
Getter for property 'databaseStructure'.Type
getIdentifierType()
Getter for property 'identifierType'.Optimizer
getOptimizer()
Getter for property 'optimizer'.void
initialize(SqlStringGenerationContext context)
Initializes this instance, in particular pre-generates SQL as necessary.protected boolean
isPhysicalSequence(JdbcEnvironment jdbcEnvironment, boolean forceTableUse)
void
registerExportables(Database database)
Register database objects used by this identifier generator, for example, a sequence or tables.boolean
supportsBulkInsertionIdentifierGeneration()
Given the configuration of this generator, is identifier generation as part of bulk insertion supported?-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.hibernate.generator.BeforeExecutionGenerator
generatedOnExecution
-
Methods inherited from interface org.hibernate.generator.Generator
generatesOnInsert, generatesOnUpdate, generatesSometimes
-
Methods inherited from interface org.hibernate.id.IdentifierGenerator
generate, getEventTypes, supportsJdbcBatchInserts
-
-
-
-
Field Detail
-
SEQUENCE_PARAM
public static final String SEQUENCE_PARAM
Indicates the name of the sequence (or table) to use. The implicit value is based on the entity / collection-role name- See Also:
- Constant Field Values
-
ALT_SEQUENCE_PARAM
public static final String ALT_SEQUENCE_PARAM
- See Also:
- Constant Field Values
-
CONFIG_SEQUENCE_PER_ENTITY_SUFFIX
public static final String CONFIG_SEQUENCE_PER_ENTITY_SUFFIX
Specifies the suffix to use for an implicit sequence name - appended to the entity-name / collection-role- See Also:
- Constant Field Values
-
DEF_SEQUENCE_SUFFIX
public static final String DEF_SEQUENCE_SUFFIX
The default value forCONFIG_SEQUENCE_PER_ENTITY_SUFFIX
- See Also:
- Constant Field Values
-
FORCE_TBL_PARAM
public static final String FORCE_TBL_PARAM
A flag to force using a table as the underlying structure rather than a sequence.- See Also:
- Constant Field Values
-
VALUE_COLUMN_PARAM
public static final String VALUE_COLUMN_PARAM
Indicates the name of the column holding the identifier values. The default value is "next_val"- See Also:
- Constant Field Values
-
DEF_VALUE_COLUMN
public static final String DEF_VALUE_COLUMN
The default value forVALUE_COLUMN_PARAM
- See Also:
- Constant Field Values
-
-
Method Detail
-
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 interfaceOptimizableGenerator
- Returns:
- Value for property 'optimizer'.
-
getIdentifierType
public Type getIdentifierType()
Getter for property 'identifierType'.- Returns:
- Value for property 'identifierType'.
-
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 interfaceConfigurable
- Specified by:
configure
in interfaceIdentifierGenerator
- Parameters:
type
- The id property type descriptorparameters
- param values, keyed by parameter nameserviceRegistry
- Access to service that may be needed.- Throws:
MappingException
- If configuration fails.
-
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 interfaceExportableProducer
- Specified by:
registerExportables
in interfaceIdentifierGenerator
- Parameters:
database
- The database instance
-
initialize
public void initialize(SqlStringGenerationContext context)
Description copied from interface:IdentifierGenerator
Initializes this instance, in particular pre-generates SQL as necessary.This method is called after
IdentifierGenerator.registerExportables(Database)
, and before first use.- Specified by:
initialize
in interfaceIdentifierGenerator
- Parameters:
context
- A context to help generate SQL strings
-
determineSequenceName
protected QualifiedName determineSequenceName(Properties params, Dialect dialect, 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).dialect
- The dialect in effectjdbcEnv
- 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 thedatabase 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 String 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
- Thedetermined increment size
- Returns:
- The optimizer strategy (name)
-
determineAdjustedIncrementSize
protected int determineAdjustedIncrementSize(String 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
- Thedetermined 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 propertyparams
- 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 interfaceIdentifierGenerator
- Parameters:
session
- The session from which the request originatesobject
- 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 interfaceBulkInsertionCapableIdentifierGenerator
- 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 interfaceBulkInsertionCapableIdentifierGenerator
- Returns:
- The identifier value generation fragment (SQL).
null
indicates that no fragment is needed.
-
-