public class SequenceStyleGenerator extends 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 the DatabaseStructure
interface internally.
<p/>
<b>NOTE</b> that by default we utilize a single database sequence for all
generators. The configuration parameter CONFIG_PREFER_SEQUENCE_PER_ENTITY
can be used to create dedicated sequence for each entity based on its name.
Sequence suffix can be controlled with CONFIG_SEQUENCE_PER_ENTITY_SUFFIX
option.
<p/>
General configuration parameters:
<table>
<tr>
<td><b>NAME</b></td>
<td><b>DEFAULT</b></td>
<td><b>DESCRIPTION</b></td>
</tr>
<tr>
<td>SEQUENCE_PARAM
</td>
<td>DEF_SEQUENCE_NAME
</td>
<td>The name of the sequence/table to use to store/retrieve values</td>
</tr>
<tr>
<td>INITIAL_PARAM
</td>
<td>DEFAULT_INITIAL_VALUE
</td>
<td>The initial value to be stored for the given segment; the effect in terms of storage varies based on Optimizer
and DatabaseStructure
</td>
</tr>
<tr>
<td>INCREMENT_PARAM
</td>
<td>DEFAULT_INCREMENT_SIZE
</td>
<td>The increment size for the underlying segment; the effect in terms of storage varies based on Optimizer
and DatabaseStructure
</td>
</tr>
<tr>
<td>OPT_PARAM
</td>
<td><i>depends on defined increment size</i></td>
<td>Allows explicit definition of which optimization strategy to use</td>
</tr>
<tr>
<td>FORCE_TBL_PARAM
</td>
<td><b><i>false</i></b></td>
<td>Allows explicit definition of which optimization strategy to use</td>
</tr>
</table>
<p/>
Configuration parameters used specifically when the underlying structure is a table:
<table>
<tr>
<td><b>NAME</b></td>
<td><b>DEFAULT</b></td>
<td><b>DESCRIPTION</b></td>
</tr>
<tr>
<td>VALUE_COLUMN_PARAM
</td>
<td>DEF_VALUE_COLUMN
</td>
<td>The name of column which holds the sequence value for the given segment</td>
</tr>
</table>
Modifier and Type | Field and Description |
---|---|
static String |
CONFIG_PREFER_SEQUENCE_PER_ENTITY
Used to create dedicated sequence for each entity based on the entity name.
|
static 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 String |
DEF_SEQUENCE_NAME
The default value for
SEQUENCE_PARAM , in the absence of any CONFIG_PREFER_SEQUENCE_PER_ENTITY
setting. |
static String |
DEF_SEQUENCE_SUFFIX
The default value for
CONFIG_SEQUENCE_PER_ENTITY_SUFFIX |
static String |
DEF_VALUE_COLUMN
The default value for
VALUE_COLUMN_PARAM |
static int |
DEFAULT_INCREMENT_SIZE
The default value for
INCREMENT_PARAM |
static int |
DEFAULT_INITIAL_VALUE
The default value for
INITIAL_PARAM |
static String |
FORCE_TBL_PARAM
A flag to force using a table as the underlying structure rather than a sequence.
|
static String |
INCREMENT_PARAM
Indicates the increment size to use.
|
static String |
INITIAL_PARAM
Indicates the initial value to use.
|
static String |
OPT_PARAM
Indicates the optimizer to use, either naming a
Optimizer implementation class or naming
a StandardOptimizerDescriptor by name |
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.
|
CATALOG, IDENTIFIER_NORMALIZER, PK, SCHEMA, TABLE, TABLES
ENTITY_NAME, GENERATOR_NAME, JPA_ENTITY_NAME
Constructor and Description |
---|
SequenceStyleGenerator() |
Modifier and Type | Method and 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 params,
ServiceRegistry serviceRegistry)
Configure this instance, given the value of parameters
specified by the user as <tt><param></tt> 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(Dialect dialect)
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.
|
Serializable |
generate(SharedSessionContractImplementor session,
Object object)
Generate a new identifier.
|
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 the
Database |
String[] |
sqlCreateStrings(Dialect dialect)
The SQL required to create the underlying database objects.
|
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?
<p/>
IMPL NOTE : Mainly here to allow stuff like SequenceStyleGenerator which can support this based on
configuration
|
public static final String SEQUENCE_PARAM
Indicates the name of the sequence (or table) to use. The default value is DEF_SEQUENCE_NAME
,
although CONFIG_PREFER_SEQUENCE_PER_ENTITY
effects the default as well.
public static final String DEF_SEQUENCE_NAME
The default value for SEQUENCE_PARAM
, in the absence of any CONFIG_PREFER_SEQUENCE_PER_ENTITY
setting.
public static final String INITIAL_PARAM
Indicates the initial value to use. The default value is DEFAULT_INITIAL_VALUE
public static final int DEFAULT_INITIAL_VALUE
The default value for INITIAL_PARAM
public static final String INCREMENT_PARAM
Indicates the increment size to use. The default value is DEFAULT_INCREMENT_SIZE
public static final int DEFAULT_INCREMENT_SIZE
The default value for INCREMENT_PARAM
public static final String CONFIG_PREFER_SEQUENCE_PER_ENTITY
Used to create dedicated sequence for each entity based on the entity name. Sequence suffix can be
controlled with CONFIG_SEQUENCE_PER_ENTITY_SUFFIX
option.
public static final 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 with CONFIG_PREFER_SEQUENCE_PER_ENTITY
.
public static final String DEF_SEQUENCE_SUFFIX
The default value for CONFIG_SEQUENCE_PER_ENTITY_SUFFIX
public static final String OPT_PARAM
Indicates the optimizer to use, either naming a Optimizer
implementation class or naming
a StandardOptimizerDescriptor
by name
public static final String FORCE_TBL_PARAM
A flag to force using a table as the underlying structure rather than a sequence.
public static final String VALUE_COLUMN_PARAM
Indicates the name of the column holding the identifier values. The default value is DEF_VALUE_COLUMN
public static final String DEF_VALUE_COLUMN
The default value for VALUE_COLUMN_PARAM
public DatabaseStructure getDatabaseStructure()
Getter for property 'databaseStructure'.
public Optimizer getOptimizer()
Getter for property 'optimizer'.
public Type getIdentifierType()
Getter for property 'identifierType'.
public void configure(Type type, Properties params, ServiceRegistry serviceRegistry) throws MappingException
Configurable
Configure this instance, given the value of parameters specified by the user as <tt><param></tt> elements. This method is called just once, following instantiation.
configure
in interface Configurable
type
- The id property type descriptorparams
- param values, keyed by parameter nameserviceRegistry
- Access to service that may be needed.MappingException
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.
<p/>
Called during configuration
.
params
- The params supplied in the generator config (plus some standard useful extras).dialect
- The dialect in effectjdbcEnv
- The JdbcEnvironmentprotected Identifier determineValueColumnName(Properties params, JdbcEnvironment jdbcEnvironment)
Determine the name of the column used to store the generator value in
the db.
<p/>
Called during configuration
<b>when resolving to a
physical table</b>.
params
- The params supplied in the generator config (plus some standard useful extras).jdbcEnvironment
- The JDBC environmentprotected int determineInitialValue(Properties params)
Determine the initial sequence value to use. This value is used when
initializing the database structure
(i.e. sequence/table).
<p/>
Called during configuration
.
params
- The params supplied in the generator config (plus some standard useful extras).protected int determineIncrementSize(Properties params)
Determine the increment size to be applied. The exact implications of
this value depends on the optimizer
being used.
<p/>
Called during configuration
.
params
- The params supplied in the generator config (plus some standard useful extras).protected String determineOptimizationStrategy(Properties params, int incrementSize)
Determine the optimizer to use.
<p/>
Called during configuration
.
params
- The params supplied in the generator config (plus some standard useful extras).incrementSize
- The determined increment size
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.
optimizationStrategy
- The optimizer strategy (name)incrementSize
- The determined increment size
protected DatabaseStructure buildDatabaseStructure(Type type, Properties params, JdbcEnvironment jdbcEnvironment, boolean forceTableUse, QualifiedName sequenceName, int initialValue, int incrementSize)
Build the database structure.
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).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)
public Serializable generate(SharedSessionContractImplementor session, Object object) throws HibernateException
IdentifierGenerator
Generate a new identifier.
generate
in interface IdentifierGenerator
session
- The session from which the request originatesobject
- the entity or collection (idbag) for which the id is being generatedHibernateException
- Indicates trouble generating the identifierpublic Object generatorKey()
PersistentIdentifierGenerator
Return a key unique to the underlying database objects. Prevents us from trying to create/remove them multiple times.
generatorKey
in interface PersistentIdentifierGenerator
public String[] sqlCreateStrings(Dialect dialect) throws HibernateException
PersistentIdentifierGenerator
The SQL required to create the underlying database objects.
sqlCreateStrings
in interface PersistentIdentifierGenerator
dialect
- The dialect against which to generate the create command(s)HibernateException
- problem creating the create command(s)public String[] sqlDropStrings(Dialect dialect) throws HibernateException
PersistentIdentifierGenerator
The SQL required to remove the underlying database objects.
sqlDropStrings
in interface PersistentIdentifierGenerator
dialect
- The dialect against which to generate the drop command(s)HibernateException
- problem creating the drop command(s)public boolean supportsBulkInsertionIdentifierGeneration()
BulkInsertionCapableIdentifierGenerator
Given the configuration of this generator, is identifier generation as part of bulk insertion supported? <p/> IMPL NOTE : Mainly here to allow stuff like SequenceStyleGenerator which can support this based on configuration
supportsBulkInsertionIdentifierGeneration
in interface BulkInsertionCapableIdentifierGenerator
true
if bulk insertions are supported; false
otherwise.public String determineBulkInsertionIdentifierGenerationSelectFragment(Dialect dialect)
BulkInsertionCapableIdentifierGenerator
Return the select expression fragment, if any, that generates the identifier values.
determineBulkInsertionIdentifierGenerationSelectFragment
in interface BulkInsertionCapableIdentifierGenerator
dialect
- The dialect against which the insert will be performed.null
indicates that no fragment is needed.public void registerExportables(Database database)
ExportableProducer
Register the contained exportable things to the Database
registerExportables
in interface ExportableProducer
database
- The database instanceCopyright © 2001-2018 Red Hat, Inc. All Rights Reserved.