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.id.IdentifierGenerator, org.hibernate.boot.model.relational.ExportableProducer, org.hibernate.id.Configurable, org.hibernate.id.PersistentIdentifierGenerator
Imports
java.io.Serializable, java.util.Objects, .Properties, org.hibernate.HibernateException, .MappingException, org.hibernate.boot.model.naming.Identifier, org.hibernate.boot.model.relational.Database, .QualifiedName, .QualifiedNameParser, .SqlStringGenerationContext, org.hibernate.cfg.AvailableSettings, org.hibernate.dialect.Dialect, org.hibernate.engine.config.spi.ConfigurationService, .StandardConverters, 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.internal.util.config.ConfigurationHelper, org.hibernate.service.ServiceRegistry, org.hibernate.tool.schema.extract.spi.SequenceInformation, org.hibernate.type.Type, org.jboss.logging.Logger

Deprecated

as a consequence of PersistentIdentifierGenerator and BulkInsertionCapableIdentifierGenerator being deprecated. See corresponding docs for further information.

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.

Note

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.

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 and DatabaseStructure
INCREMENT_PARAM DEFAULT_INCREMENT_SIZE The increment size for the underlying segment; the effect in terms of storage varies based on Optimizer and DatabaseStructure
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

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

NAME DEFAULT DESCRIPTION
VALUE_COLUMN_PARAM DEF_VALUE_COLUMN The name of column which holds the sequence value for the given segment
Authors
Steve Ebersole, Lukasz Antoniak (lukasz dot antoniak at gmail dot com)

Field Summary

Modifier and TypeField and Description
public static final String
CONFIG_PREFER_SEQUENCE_PER_ENTITY

Used to create dedicated sequence for each entity based on the entity name.

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.

private DatabaseStructure
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
public static final String
DEF_VALUE_COLUMN

The default value for VALUE_COLUMN_PARAM

public static final int
public static final int
DEFAULT_INITIAL_VALUE

The default value for INITIAL_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
public static final String
INCREMENT_PARAM

Indicates the increment size to use.

public static final String
INITIAL_PARAM

Indicates the initial value to use.

private static final CoreMessageLogger
public static final String
OPT_PARAM

Indicates the optimizer to use, either naming a Optimizer implementation class or naming a StandardOptimizerDescriptor by name

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
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
params
,
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
(String
The optimizer strategy (name)
optimizationStrategy
,
int incrementSize)

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

public String
determineBulkInsertionIdentifierGenerationSelectFragment(SqlStringGenerationContext
A context for SQL string generation.
context
)

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

Return the select expression fragment, if any, that generates the identifier values.
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 String

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 Serializable
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 Object
generatorKey()

Overrides default org.hibernate.id.PersistentIdentifierGenerator.generatorKey.

Deprecated
Return a key unique to the underlying database objects.
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
()

Getter for property 'optimizer'.

private Long

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.

Register database objects used by this identifier generator, e.g. sequences, tables, etc.

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

public boolean
supportsBulkInsertionIdentifierGeneration()

Implements 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

CONFIG_PREFER_SEQUENCE_PER_ENTITYback to summary
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.

Annotations
@SuppressWarnings:WeakerAccess
CONFIG_SEQUENCE_PER_ENTITY_SUFFIXback to summary
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.

Annotations
@SuppressWarnings:WeakerAccess
databaseStructureback to summary
private DatabaseStructure databaseStructure
DEF_SEQUENCE_NAMEback to summary
public static final String DEF_SEQUENCE_NAME

The default value for SEQUENCE_PARAM, in the absence of any CONFIG_PREFER_SEQUENCE_PER_ENTITY setting.

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

Annotations
@SuppressWarnings:WeakerAccess
DEFAULT_INCREMENT_SIZEback to summary
public static final int DEFAULT_INCREMENT_SIZE

The default value for INCREMENT_PARAM

DEFAULT_INITIAL_VALUEback to summary
public static final int DEFAULT_INITIAL_VALUE

The default value for INITIAL_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
INCREMENT_PARAMback to summary
public static final String INCREMENT_PARAM

Indicates the increment size to use. The default value is DEFAULT_INCREMENT_SIZE

INITIAL_PARAMback to summary
public static final String INITIAL_PARAM

Indicates the initial value to use. The default value is DEFAULT_INITIAL_VALUE

LOGback to summary
private static final CoreMessageLogger LOG
OPT_PARAMback to summary
public static final String OPT_PARAM

Indicates the optimizer to use, either naming a Optimizer implementation class or naming a StandardOptimizerDescriptor by name

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 default value is DEF_SEQUENCE_NAME, although CONFIG_PREFER_SEQUENCE_PER_ENTITY effects the default as well.

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 DEF_VALUE_COLUMN

Annotations
@SuppressWarnings:WeakerAccess

Constructor Detail

SequenceStyleGeneratorback to summary
public SequenceStyleGenerator()

Method Detail

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).

Annotations
@SuppressWarnings:WeakerAccess
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)
Annotations
@SuppressWarnings:WeakerAccess
configureback to summary
public void configure(Type type, Properties params, 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

params:Properties

param values, keyed by parameter name

serviceRegistry:ServiceRegistry

Access to service that may be needed.

Annotations
@Override
Exceptions
MappingException:
If configuration fails.
determineAdjustedIncrementSizeback to summary
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:String

The optimizer strategy (name)

incrementSize:int

The determined increment size

Returns:int

The adjusted increment size.

Annotations
@SuppressWarnings:WeakerAccess
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.

Parameters
context:SqlStringGenerationContext

A context for SQL string generation.

Returns:String

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

Annotations
@Override
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

Annotations
@SuppressWarnings:WeakerAccess
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

Annotations
@SuppressWarnings:WeakerAccess
determineOptimizationStrategyback to summary
protected String 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:String

The optimizer strategy (name)

Annotations
@SuppressWarnings:WeakerAccess
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, WeakerAccess
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

Annotations
@SuppressWarnings:UnusedParameters, WeakerAccess
generateback to summary
public Serializable 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:Serializable

a new identifier

Annotations
@Override
Exceptions
HibernateException:
Indicates trouble generating the identifier
generatorKeyback to summary
public Object generatorKey()

Overrides default org.hibernate.id.PersistentIdentifierGenerator.generatorKey.

Doc from org.hibernate.id.PersistentIdentifierGenerator.generatorKey.

Deprecated

Return a key unique to the underlying database objects. Prevents us from trying to create/remove them multiple times.

Returns:Object

Object an identifying key for this generator

Annotations
@Override
@Deprecated
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()

Getter for property 'optimizer'.

Returns:Optimizer

Value for property 'optimizer'.

getSequenceIncrementValueback to summary
private Long 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:Long

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), 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.

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

Register database objects used by this identifier generator, e.g. sequences, tables, etc.

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()

Implements 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?

IMPL NOTE : Mainly here to allow stuff like SequenceStyleGenerator which *can* support this based on configuration

Returns:boolean

true if bulk insertions are supported; false otherwise.

Annotations
@Override