Top Description Fields Constructors Methods
org.hibernate.id.enhanced

public Class SequenceStyleGenerator

extends Object
implements PersistentIdentifierGenerator, BulkInsertionCapableIdentifierGenerator
Class Inheritance
All Implemented Interfaces
org.hibernate.id.BulkInsertionCapableIdentifierGenerator, org.hibernate.generator.Generator, java.io.Serializable, org.hibernate.id.PersistentIdentifierGenerator, org.hibernate.id.OptimizableGenerator, org.hibernate.boot.model.relational.ExportableProducer, org.hibernate.id.factory.spi.StandardGenerator, org.hibernate.id.IdentifierGenerator, org.hibernate.id.Configurable, org.hibernate.generator.BeforeExecutionGenerator
Imports
java.util.Objects, .Properties, org.hibernate.HibernateException, .MappingException, org.hibernate.boot.model.naming.Identifier, org.hibernate.boot.model.relational.Database, .QualifiedName, .QualifiedNameParser, .QualifiedSequenceName, .SqlStringGenerationContext, org.hibernate.boot.registry.selector.spi.StrategySelector, org.hibernate.cfg.AvailableSettings, org.hibernate.dialect.Dialect, org.hibernate.engine.config.spi.ConfigurationService, org.hibernate.engine.jdbc.env.spi.JdbcEnvironment, org.hibernate.engine.spi.SharedSessionContractImplementor, org.hibernate.id.BulkInsertionCapableIdentifierGenerator, .IdentifierGenerator, .PersistentIdentifierGenerator, .SequenceMismatchStrategy, org.hibernate.internal.CoreMessageLogger, org.hibernate.internal.util.StringHelper, org.hibernate.service.ServiceRegistry, org.hibernate.tool.schema.extract.spi.SequenceInformation, org.hibernate.type.Type, org.jboss.logging.Logger

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
Authors
Steve Ebersole, Lukasz Antoniak

Field Summary

Modifier and TypeField and Description
public static final String
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

private DatabaseStructure
public static final String
public static final String
DEF_VALUE_COLUMN

The default value for VALUE_COLUMN_PARAM

public static final String
FORCE_TBL_PARAM

A flag to force using a table as the underlying structure rather than a sequence.

private Type
private static final CoreMessageLogger
private Optimizer
public static final String
SEQUENCE_PARAM

Indicates the name of the sequence (or table) to use.

public static final String
VALUE_COLUMN_PARAM

Indicates the name of the column holding the identifier values.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private int
adjustIncrementSize(JdbcEnvironment jdbcEnvironment, QualifiedName sequenceName, int incrementSize, boolean physicalSequence, OptimizerDescriptor optimizationStrategy, ServiceRegistry serviceRegistry)

protected DatabaseStructure

Returns:

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

public void
configure(Type
The id property type descriptor
type
,
Properties
param values, keyed by parameter name
parameters
,
ServiceRegistry
Access to service that may be needed.
serviceRegistry
)

Overrides default org.hibernate.id.IdentifierGenerator.configure.

Configure this instance, given the value of parameters specified by the user as <param> elements.
protected int

Returns:

The adjusted increment size.
determineAdjustedIncrementSize
(OptimizerDescriptor
The optimizer strategy (name)
optimizationStrategy
,
int incrementSize)

In certain cases we need to adjust the increment size based on the selected optimizer.

public String
private String
private QualifiedName
determineImplicitName(Identifier catalog, Identifier schema, Properties params, ServiceRegistry serviceRegistry)

protected int

Returns:

The increment size
determineIncrementSize
(Properties
The params supplied in the generator config (plus some standard useful extras).
params
)

Determine the increment size to be applied.

protected int

Returns:

The initial value
determineInitialValue
(Properties
The params supplied in the generator config (plus some standard useful extras).
params
)

Determine the initial sequence value to use.

protected OptimizerDescriptor

Returns:

The optimizer strategy (name)
determineOptimizationStrategy
(Properties
The params supplied in the generator config (plus some standard useful extras).
params
,
int incrementSize)

Determine the optimizer to use.

protected QualifiedName

Returns:

The sequence name
determineSequenceName
(Properties
The params supplied in the generator config (plus some standard useful extras).
params
,
Dialect
The dialect in effect
dialect
,
JdbcEnvironment
The JdbcEnvironment
jdbcEnv
,
ServiceRegistry serviceRegistry)

Determine the name of the sequence (or table if this resolves to a physical table) to use.

protected Identifier

Returns:

The value column name
determineValueColumnName
(Properties
The params supplied in the generator config (plus some standard useful extras).
params
,
JdbcEnvironment
The JDBC environment
jdbcEnvironment
)

Determine the name of the column used to store the generator value in the db.

public Object
generate(SharedSessionContractImplementor
The session from which the request originates
session
,
Object
the entity or collection (idbag) for which the id is being generated
object
)

Implements org.hibernate.id.IdentifierGenerator.generate.

Generate a new identifier.
public DatabaseStructure

Returns:

Value for property 'databaseStructure'.
getDatabaseStructure
()

Getter for property 'databaseStructure'.

public Type

Returns:

Value for property 'identifierType'.
getIdentifierType
()

Getter for property 'identifierType'.

public Optimizer

Returns:

Value for property 'optimizer'.
getOptimizer
()

Implements org.hibernate.id.OptimizableGenerator.getOptimizer.

Getter for property 'optimizer'.
private Number

Returns:

sequence increment value
getSequenceIncrementValue
(JdbcEnvironment
the current JdbcEnvironment
jdbcEnvironment
,
String
sequence name
sequenceName
)

Get the database sequence increment value from the associated SequenceInformation object.

public void
initialize(SqlStringGenerationContext
A context to help generate SQL strings
context
)

Overrides default org.hibernate.id.IdentifierGenerator.initialize.

Initializes this instance, in particular pre-generates SQL as necessary.
protected boolean
isPhysicalSequence(JdbcEnvironment jdbcEnvironment, boolean forceTableUse)

public void
registerExportables(Database
The database instance
database
)

Overrides default org.hibernate.id.IdentifierGenerator.registerExportables.

Implements org.hibernate.boot.model.relational.ExportableProducer.registerExportables.

Register database objects used by this identifier generator, for example, a sequence or tables.
public boolean
supportsBulkInsertionIdentifierGeneration()

Overrides default org.hibernate.id.BulkInsertionCapableIdentifierGenerator.supportsBulkInsertionIdentifierGeneration.

Given the configuration of this generator, is identifier generation as part of bulk insertion supported?
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

ALT_SEQUENCE_PARAMback to summary
public static final String ALT_SEQUENCE_PARAM
CONFIG_SEQUENCE_PER_ENTITY_SUFFIXback to summary
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

databaseStructureback to summary
private DatabaseStructure databaseStructure
DEF_SEQUENCE_SUFFIXback to summary
public static final String DEF_SEQUENCE_SUFFIX

The default value for CONFIG_SEQUENCE_PER_ENTITY_SUFFIX

DEF_VALUE_COLUMNback to summary
public static final String DEF_VALUE_COLUMN

The default value for VALUE_COLUMN_PARAM

FORCE_TBL_PARAMback to summary
public static final String FORCE_TBL_PARAM

A flag to force using a table as the underlying structure rather than a sequence.

identifierTypeback to summary
private Type identifierType
LOGback to summary
private static final CoreMessageLogger LOG
optimizerback to summary
private Optimizer optimizer
SEQUENCE_PARAMback to summary
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

VALUE_COLUMN_PARAMback to summary
public static final String VALUE_COLUMN_PARAM

Indicates the name of the column holding the identifier values. The default value is next_val

Constructor Detail

SequenceStyleGeneratorback to summary
public SequenceStyleGenerator()

Method Detail

adjustIncrementSizeback to summary
private int adjustIncrementSize(JdbcEnvironment jdbcEnvironment, QualifiedName sequenceName, int incrementSize, boolean physicalSequence, OptimizerDescriptor optimizationStrategy, ServiceRegistry serviceRegistry)
buildDatabaseStructureback to summary
protected DatabaseStructure buildDatabaseStructure(Type type, Properties params, JdbcEnvironment jdbcEnvironment, boolean forceTableUse, QualifiedName sequenceName, int initialValue, int incrementSize)

Build the database structure.

Parameters
type:Type

The Hibernate type of the identifier property

params:Properties

The params supplied in the generator config (plus some standard useful extras).

jdbcEnvironment:JdbcEnvironment

The JDBC environment in which the sequence will be used.

forceTableUse:boolean

Should a table be used even if the dialect supports sequences?

sequenceName:QualifiedName

The name to use for the sequence or table.

initialValue:int

The initial value.

incrementSize:int

the increment size to use (after any adjustments).

Returns:DatabaseStructure

An abstraction for the actual database structure in use (table vs. sequence).

buildSequenceStructureback to summary
protected DatabaseStructure buildSequenceStructure(Type type, Properties params, JdbcEnvironment jdbcEnvironment, QualifiedName sequenceName, int initialValue, int incrementSize)
buildTableStructureback to summary
protected DatabaseStructure buildTableStructure(Type type, Properties params, JdbcEnvironment jdbcEnvironment, QualifiedName sequenceName, int initialValue, int incrementSize)
configureback to summary
public void configure(Type type, Properties parameters, ServiceRegistry serviceRegistry) throws MappingException

Overrides default org.hibernate.id.IdentifierGenerator.configure.

Doc from org.hibernate.id.IdentifierGenerator.configure.

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 registerExportables(Database).

Parameters
type:Type

The id property type descriptor

parameters:Properties

param values, keyed by parameter name

serviceRegistry:ServiceRegistry

Access to service that may be needed.

Annotations
@Override
determineAdjustedIncrementSizeback to summary
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:OptimizerDescriptor

The optimizer strategy (name)

incrementSize:int

The determined increment size

Returns:int

The adjusted increment size.

determineBulkInsertionIdentifierGenerationSelectFragmentback to summary
public String determineBulkInsertionIdentifierGenerationSelectFragment(SqlStringGenerationContext context)

Overrides default org.hibernate.id.BulkInsertionCapableIdentifierGenerator.determineBulkInsertionIdentifierGenerationSelectFragment.

Doc from org.hibernate.id.BulkInsertionCapableIdentifierGenerator.determineBulkInsertionIdentifierGenerationSelectFragment.

Return the select expression fragment, if any, that generates the identifier values.

Returns:String

The identifier value generation fragment (SQL). null indicates that no fragment is needed.

Annotations
@Override
determineContributorback to summary
private String determineContributor(Properties params)
determineImplicitNameback to summary
private QualifiedName determineImplicitName(Identifier catalog, Identifier schema, Properties params, ServiceRegistry serviceRegistry)
determineIncrementSizeback to summary
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:Properties

The params supplied in the generator config (plus some standard useful extras).

Returns:int

The increment size

determineInitialValueback to summary
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:Properties

The params supplied in the generator config (plus some standard useful extras).

Returns:int

The initial value

determineOptimizationStrategyback to summary
protected OptimizerDescriptor determineOptimizationStrategy(Properties params, int incrementSize)

Determine the optimizer to use.

Called during configuration.

Parameters
params:Properties

The params supplied in the generator config (plus some standard useful extras).

incrementSize:int

The determined increment size

Returns:OptimizerDescriptor

The optimizer strategy (name)

determineSequenceNameback to summary
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:Properties

The params supplied in the generator config (plus some standard useful extras).

dialect:Dialect

The dialect in effect

jdbcEnv:JdbcEnvironment

The JdbcEnvironment

Returns:QualifiedName

The sequence name

Annotations
@SuppressWarnings:UnusedParameters
determineValueColumnNameback to summary
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:Properties

The params supplied in the generator config (plus some standard useful extras).

jdbcEnvironment:JdbcEnvironment

The JDBC environment

Returns:Identifier

The value column name

generateback to summary
public Object generate(SharedSessionContractImplementor session, Object object) throws HibernateException

Implements org.hibernate.id.IdentifierGenerator.generate.

Doc from org.hibernate.id.IdentifierGenerator.generate.

Generate a new identifier.

Parameters
session:SharedSessionContractImplementor

The session from which the request originates

object:Object

the entity or collection (idbag) for which the id is being generated

Returns:Object

a new identifier

Annotations
@Override
Exceptions
HibernateException:
Indicates trouble generating the identifier
getDatabaseStructureback to summary
public DatabaseStructure getDatabaseStructure()

Getter for property 'databaseStructure'.

Returns:DatabaseStructure

Value for property 'databaseStructure'.

getIdentifierTypeback to summary
public Type getIdentifierType()

Getter for property 'identifierType'.

Returns:Type

Value for property 'identifierType'.

getOptimizerback to summary
public Optimizer getOptimizer()

Implements org.hibernate.id.OptimizableGenerator.getOptimizer.

Getter for property 'optimizer'.

Returns:Optimizer

Value for property 'optimizer'.

Annotations
@Override
getSequenceIncrementValueback to summary
private Number getSequenceIncrementValue(JdbcEnvironment jdbcEnvironment, String sequenceName)

Get the database sequence increment value from the associated SequenceInformation object.

Parameters
jdbcEnvironment:JdbcEnvironment

the current JdbcEnvironment

sequenceName:String

sequence name

Returns:Number

sequence increment value

initializeback to summary
public void initialize(SqlStringGenerationContext context)

Overrides default org.hibernate.id.IdentifierGenerator.initialize.

Doc from org.hibernate.id.IdentifierGenerator.initialize.

Initializes this instance, in particular pre-generates SQL as necessary.

This method is called after registerExportables(Database), and before first use.

Parameters
context:SqlStringGenerationContext

A context to help generate SQL strings

Annotations
@Override
isPhysicalSequenceback to summary
protected boolean isPhysicalSequence(JdbcEnvironment jdbcEnvironment, boolean forceTableUse)
registerExportablesback to summary
public void registerExportables(Database database)

Overrides default org.hibernate.id.IdentifierGenerator.registerExportables.

Implements org.hibernate.boot.model.relational.ExportableProducer.registerExportables.

Doc from org.hibernate.id.IdentifierGenerator.registerExportables.

Register database objects used by this identifier generator, for example, a sequence or tables.

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

Parameters
database:Database

The database instance

Annotations
@Override
supportsBulkInsertionIdentifierGenerationback to summary
public boolean supportsBulkInsertionIdentifierGeneration()

Overrides default org.hibernate.id.BulkInsertionCapableIdentifierGenerator.supportsBulkInsertionIdentifierGeneration.

Doc from org.hibernate.id.BulkInsertionCapableIdentifierGenerator.supportsBulkInsertionIdentifierGeneration.

Given the configuration of this generator, is identifier generation as part of bulk insertion supported?

Returns:boolean

true if bulk insertions are supported; false otherwise.

Annotations
@Override