Package org.hibernate.id.enhanced
Class SequenceStyleGenerator
- java.lang.Object
-
- org.hibernate.id.enhanced.SequenceStyleGenerator
-
- All Implemented Interfaces:
ExportableProducer
,BulkInsertionCapableIdentifierGenerator
,Configurable
,IdentifierGenerator
,PersistentIdentifierGenerator
- Direct Known Subclasses:
OrderedSequenceGenerator
public class SequenceStyleGenerator extends java.lang.Object implements PersistentIdentifierGenerator, BulkInsertionCapableIdentifierGenerator, Configurable
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. NOTE that by default we utilize a single database sequence for all generators. The configuration parameterCONFIG_PREFER_SEQUENCE_PER_ENTITY
can be used to create dedicated sequence for each entity based on its name. Sequence suffix can be controlled withCONFIG_SEQUENCE_PER_ENTITY_SUFFIX
option. General configuration parameters:NAME DEFAULT DESCRIPTION SEQUENCE_PARAM
DEF_SEQUENCE_NAME
The name of the sequence/table to use to store/retrieve values INITIAL_PARAM
DEFAULT_INITIAL_VALUE
The initial value to be stored for the given segment; the effect in terms of storage varies based on Optimizer
andDatabaseStructure
INCREMENT_PARAM
DEFAULT_INCREMENT_SIZE
The increment size for the underlying segment; the effect in terms of storage varies based on Optimizer
andDatabaseStructure
OPT_PARAM
depends on defined increment size Allows explicit definition of which optimization strategy to use FORCE_TBL_PARAM
false Allows explicit definition of which optimization strategy to use NAME DEFAULT DESCRIPTION VALUE_COLUMN_PARAM
DEF_VALUE_COLUMN
The name of column which holds the sequence value for the given segment
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
CONFIG_PREFER_SEQUENCE_PER_ENTITY
Used to create dedicated sequence for each entity based on the entity name.static java.lang.String
CONFIG_SEQUENCE_PER_ENTITY_SUFFIX
Indicates the suffix to use in naming the identifier sequence/table name, by appending the suffix to the name of the entity.static java.lang.String
DEF_SEQUENCE_NAME
The default value forSEQUENCE_PARAM
, in the absence of anyCONFIG_PREFER_SEQUENCE_PER_ENTITY
setting.static java.lang.String
DEF_SEQUENCE_SUFFIX
The default value forCONFIG_SEQUENCE_PER_ENTITY_SUFFIX
static java.lang.String
DEF_VALUE_COLUMN
The default value forVALUE_COLUMN_PARAM
static int
DEFAULT_INCREMENT_SIZE
The default value forINCREMENT_PARAM
static int
DEFAULT_INITIAL_VALUE
The default value forINITIAL_PARAM
static java.lang.String
FORCE_TBL_PARAM
A flag to force using a table as the underlying structure rather than a sequence.static java.lang.String
INCREMENT_PARAM
Indicates the increment size to use.static java.lang.String
INITIAL_PARAM
Indicates the initial value to use.static java.lang.String
OPT_PARAM
Indicates the optimizer to use, either naming aOptimizer
implementation class or naming aStandardOptimizerDescriptor
by namestatic java.lang.String
SEQUENCE_PARAM
Indicates the name of the sequence (or table) to use.static java.lang.String
VALUE_COLUMN_PARAM
Indicates the name of the column holding the identifier values.-
Fields inherited from interface org.hibernate.id.IdentifierGenerator
ENTITY_NAME, GENERATOR_NAME, JPA_ENTITY_NAME
-
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, java.util.Properties params, JdbcEnvironment jdbcEnvironment, boolean forceTableUse, QualifiedName sequenceName, int initialValue, int incrementSize)
Build the database structure.protected DatabaseStructure
buildSequenceStructure(Type type, java.util.Properties params, JdbcEnvironment jdbcEnvironment, QualifiedName sequenceName, int initialValue, int incrementSize)
protected DatabaseStructure
buildTableStructure(Type type, java.util.Properties params, JdbcEnvironment jdbcEnvironment, QualifiedName sequenceName, int initialValue, int incrementSize)
void
configure(Type type, java.util.Properties params, ServiceRegistry serviceRegistry)
Configure this instance, given the value of parameters specified by the user as <param> elements.protected int
determineAdjustedIncrementSize(java.lang.String optimizationStrategy, int incrementSize)
In certain cases we need to adjust the increment size based on the selected optimizer.java.lang.String
determineBulkInsertionIdentifierGenerationSelectFragment(Dialect dialect)
Return the select expression fragment, if any, that generates the identifier values.protected int
determineIncrementSize(java.util.Properties params)
Determine the increment size to be applied.protected int
determineInitialValue(java.util.Properties params)
Determine the initial sequence value to use.protected java.lang.String
determineOptimizationStrategy(java.util.Properties params, int incrementSize)
Determine the optimizer to use.protected QualifiedName
determineSequenceName(java.util.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(java.util.Properties params, JdbcEnvironment jdbcEnvironment)
Determine the name of the column used to store the generator value in the db.java.io.Serializable
generate(SharedSessionContractImplementor session, java.lang.Object object)
Generate a new identifier.java.lang.Object
generatorKey()
Return a key unique to the underlying database objects.DatabaseStructure
getDatabaseStructure()
Getter for property 'databaseStructure'.Type
getIdentifierType()
Getter for property 'identifierType'.Optimizer
getOptimizer()
Getter for property 'optimizer'.void
registerExportables(Database database)
Register the contained exportable things to theDatabase
java.lang.String[]
sqlCreateStrings(Dialect dialect)
The SQL required to create the underlying database objects.java.lang.String[]
sqlDropStrings(Dialect dialect)
The SQL required to remove the underlying database objects.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.id.IdentifierGenerator
supportsJdbcBatchInserts
-
-
-
-
Field Detail
-
SEQUENCE_PARAM
public static final java.lang.String SEQUENCE_PARAM
Indicates the name of the sequence (or table) to use. The default value isDEF_SEQUENCE_NAME
, althoughCONFIG_PREFER_SEQUENCE_PER_ENTITY
effects the default as well.- See Also:
- Constant Field Values
-
DEF_SEQUENCE_NAME
public static final java.lang.String DEF_SEQUENCE_NAME
The default value forSEQUENCE_PARAM
, in the absence of anyCONFIG_PREFER_SEQUENCE_PER_ENTITY
setting.- See Also:
- Constant Field Values
-
INITIAL_PARAM
public static final java.lang.String INITIAL_PARAM
Indicates the initial value to use. The default value isDEFAULT_INITIAL_VALUE
- See Also:
- Constant Field Values
-
DEFAULT_INITIAL_VALUE
public static final int DEFAULT_INITIAL_VALUE
The default value forINITIAL_PARAM
- See Also:
- Constant Field Values
-
INCREMENT_PARAM
public static final java.lang.String INCREMENT_PARAM
Indicates the increment size to use. The default value isDEFAULT_INCREMENT_SIZE
- See Also:
- Constant Field Values
-
DEFAULT_INCREMENT_SIZE
public static final int DEFAULT_INCREMENT_SIZE
The default value forINCREMENT_PARAM
- See Also:
- Constant Field Values
-
CONFIG_PREFER_SEQUENCE_PER_ENTITY
public static final java.lang.String CONFIG_PREFER_SEQUENCE_PER_ENTITY
Used to create dedicated sequence for each entity based on the entity name. Sequence suffix can be controlled withCONFIG_SEQUENCE_PER_ENTITY_SUFFIX
option.- See Also:
- Constant Field Values
-
CONFIG_SEQUENCE_PER_ENTITY_SUFFIX
public static final java.lang.String CONFIG_SEQUENCE_PER_ENTITY_SUFFIX
Indicates the suffix to use in naming the identifier sequence/table name, by appending the suffix to the name of the entity. Used in conjunction withCONFIG_PREFER_SEQUENCE_PER_ENTITY
.- See Also:
- Constant Field Values
-
DEF_SEQUENCE_SUFFIX
public static final java.lang.String DEF_SEQUENCE_SUFFIX
The default value forCONFIG_SEQUENCE_PER_ENTITY_SUFFIX
- See Also:
- Constant Field Values
-
OPT_PARAM
public static final java.lang.String OPT_PARAM
Indicates the optimizer to use, either naming aOptimizer
implementation class or naming aStandardOptimizerDescriptor
by name- See Also:
- Constant Field Values
-
FORCE_TBL_PARAM
public static final java.lang.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 java.lang.String VALUE_COLUMN_PARAM
Indicates the name of the column holding the identifier values. The default value isDEF_VALUE_COLUMN
- See Also:
- Constant Field Values
-
DEF_VALUE_COLUMN
public static final java.lang.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'.- Returns:
- Value for property 'optimizer'.
-
getIdentifierType
public Type getIdentifierType()
Getter for property 'identifierType'.- Returns:
- Value for property 'identifierType'.
-
configure
public void configure(Type type, java.util.Properties params, ServiceRegistry serviceRegistry) throws MappingException
Description copied from interface:Configurable
Configure this instance, given the value of parameters specified by the user as <param> elements. This method is called just once, following instantiation.- Specified by:
configure
in interfaceConfigurable
- Parameters:
type
- The id property type descriptorparams
- param values, keyed by parameter nameserviceRegistry
- Access to service that may be needed.- Throws:
MappingException
-
determineSequenceName
protected QualifiedName determineSequenceName(java.util.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 duringconfiguration
.- 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(java.util.Properties params, JdbcEnvironment jdbcEnvironment)
Determine the name of the column used to store the generator value in the db. Called duringconfiguration
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(java.util.Properties params)
Determine the initial sequence value to use. This value is used when initializing thedatabase structure
(i.e. sequence/table). Called duringconfiguration
.- Parameters:
params
- The params supplied in the generator config (plus some standard useful extras).- Returns:
- The initial value
-
determineIncrementSize
protected int determineIncrementSize(java.util.Properties params)
Determine the increment size to be applied. The exact implications of this value depends on theoptimizer
being used. Called duringconfiguration
.- Parameters:
params
- The params supplied in the generator config (plus some standard useful extras).- Returns:
- The increment size
-
determineOptimizationStrategy
protected java.lang.String determineOptimizationStrategy(java.util.Properties params, int incrementSize)
Determine the optimizer to use. Called duringconfiguration
.- 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(java.lang.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, java.util.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).
-
buildSequenceStructure
protected DatabaseStructure buildSequenceStructure(Type type, java.util.Properties params, JdbcEnvironment jdbcEnvironment, QualifiedName sequenceName, int initialValue, int incrementSize)
-
buildTableStructure
protected DatabaseStructure buildTableStructure(Type type, java.util.Properties params, JdbcEnvironment jdbcEnvironment, QualifiedName sequenceName, int initialValue, int incrementSize)
-
generate
public java.io.Serializable generate(SharedSessionContractImplementor session, java.lang.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
-
generatorKey
public java.lang.Object generatorKey()
Description copied from interface:PersistentIdentifierGenerator
Return a key unique to the underlying database objects. Prevents us from trying to create/remove them multiple times.- Specified by:
generatorKey
in interfacePersistentIdentifierGenerator
- Returns:
- Object an identifying key for this generator
-
sqlCreateStrings
public java.lang.String[] sqlCreateStrings(Dialect dialect) throws HibernateException
Description copied from interface:PersistentIdentifierGenerator
The SQL required to create the underlying database objects.- Specified by:
sqlCreateStrings
in interfacePersistentIdentifierGenerator
- Parameters:
dialect
- The dialect against which to generate the create command(s)- Returns:
- The create command(s)
- Throws:
HibernateException
- problem creating the create command(s)
-
sqlDropStrings
public java.lang.String[] sqlDropStrings(Dialect dialect) throws HibernateException
Description copied from interface:PersistentIdentifierGenerator
The SQL required to remove the underlying database objects.- Specified by:
sqlDropStrings
in interfacePersistentIdentifierGenerator
- Parameters:
dialect
- The dialect against which to generate the drop command(s)- Returns:
- The drop command(s)
- Throws:
HibernateException
- problem creating the drop command(s)
-
supportsBulkInsertionIdentifierGeneration
public boolean supportsBulkInsertionIdentifierGeneration()
Description copied from interface:BulkInsertionCapableIdentifierGenerator
Given the configuration of this generator, is identifier generation as part of bulk insertion supported? IMPL NOTE : Mainly here to allow stuff like SequenceStyleGenerator which *can* support this based on configuration- Specified by:
supportsBulkInsertionIdentifierGeneration
in interfaceBulkInsertionCapableIdentifierGenerator
- Returns:
true
if bulk insertions are supported;false
otherwise.
-
determineBulkInsertionIdentifierGenerationSelectFragment
public java.lang.String determineBulkInsertionIdentifierGenerationSelectFragment(Dialect dialect)
Description copied from interface:BulkInsertionCapableIdentifierGenerator
Return the select expression fragment, if any, that generates the identifier values.- Specified by:
determineBulkInsertionIdentifierGenerationSelectFragment
in interfaceBulkInsertionCapableIdentifierGenerator
- Parameters:
dialect
- The dialect against which the insert will be performed.- Returns:
- The identifier value generation fragment (SQL).
null
indicates that no fragment is needed.
-
registerExportables
public void registerExportables(Database database)
Description copied from interface:ExportableProducer
Register the contained exportable things to theDatabase
- Specified by:
registerExportables
in interfaceExportableProducer
- Parameters:
database
- The database instance
-
-