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

public Class TableGenerator

extends Object
implements PersistentIdentifierGenerator
Class Inheritance
All Implemented Interfaces
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, org.hibernate.generator.Generator, java.io.Serializable
Imports
java.sql.Connection, .PreparedStatement, .ResultSet, .SQLException, .Types, java.util.Map, .Properties, org.hibernate.LockMode, .LockOptions, .MappingException, org.hibernate.boot.model.naming.Identifier, org.hibernate.boot.model.relational.Database, .InitCommand, .Namespace, .QualifiedName, .QualifiedNameParser, .SqlStringGenerationContext, org.hibernate.boot.registry.selector.spi.StrategySelector, org.hibernate.cfg.AvailableSettings, org.hibernate.engine.config.spi.ConfigurationService, .StandardConverters, org.hibernate.engine.jdbc.Size, org.hibernate.engine.jdbc.env.spi.JdbcEnvironment, org.hibernate.engine.jdbc.internal.FormatStyle, org.hibernate.engine.jdbc.spi.SqlStatementLogger, org.hibernate.engine.spi.SessionEventListenerManager, .SharedSessionContractImplementor, org.hibernate.event.spi.EventManager, .HibernateMonitoringEvent, org.hibernate.id.ExportableColumn, .IdentifierGeneratorHelper, .IntegralDataTypeHolder, .PersistentIdentifierGenerator, org.hibernate.internal.CoreMessageLogger, org.hibernate.internal.util.StringHelper, org.hibernate.jdbc.AbstractReturningWork, org.hibernate.mapping.Column, .PrimaryKey, .Table, org.hibernate.service.ServiceRegistry, org.hibernate.type.BasicTypeRegistry, .StandardBasicTypes, .Type, org.jboss.logging.Logger

An enhanced version of table-based id generation.

Unlike the simplistic legacy one (which was only ever intended for subclassing support) we "segment" the table into multiple values. Thus, a single table can actually serve as the persistent storage for multiple independent generators. One approach would be to segment the values by the name of the entity for which we are performing generation, which would mean that we would have a row in the generator table for each entity name. Or any configuration really; the setup is very flexible.

By default, we use a single row for all generators (the default segment). The configuration parameter prefer_entity_table_as_segment_value can be used to change that to instead default to using a row for each entity name.

Configuration parameters
Parameter name Default value Interpretation
table_name hibernate_sequences The name of the table to use to store/retrieve values
value_column_name next_val The name of column which holds the sequence value for the given segment
segment_column_name sequence_name The name of the column which holds the segment key
segment_value default The value indicating which segment is used by this generator; refers to values in the segment_column_name column
segment_value_length 255 The data length of the segment_column_name column; used for schema creation
initial_value 1 The initial value to be stored for the given segment
increment_size 50 The increment size for the underlying segment; see the discussion on Optimizer for more details.
optimizer depends on defined increment size Allows explicit definition of which optimization strategy to use
Author
Steve Ebersole

Field Summary

Modifier and TypeField and Description
private long
public static final String
CONFIG_PREFER_SEGMENT_PER_ENTITY

By default, in the absence of a segment_value setting, we use a single row for all generators.

private String
public static final String
DEF_SEGMENT_COLUMN

The default segment_column_name value

public static final int
DEF_SEGMENT_LENGTH

The default segment_value_length value

public static final String
DEF_SEGMENT_VALUE

The default segment_value value, unless CONFIG_PREFER_SEGMENT_PER_ENTITY is specified

public static final String
DEF_TABLE

The default table_name value

public static final String
DEF_VALUE_COLUMN

The default value_column_name value

private Type
private int
private int
private String
private static final CoreMessageLogger
private Optimizer
private QualifiedName
private QualifiedName
public static final String
SEGMENT_COLUMN_PARAM

The name of the column which holds the segment key.

public static final String
SEGMENT_LENGTH_PARAM

Indicates the length of the column defined by SEGMENT_COLUMN_PARAM.

public static final String
SEGMENT_VALUE_PARAM

The value indicating which segment is used by this generator, as indicated by the actual value stored in the column indicated by segment_column_name.

private String
private String
private int
private String
private boolean
public static final String
TABLE_PARAM

Configures the name of the table to use.

private String
public static final String
VALUE_COLUMN_PARAM

The name of column which holds the sequence value.

private String

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected String
buildInsertQuery(String formattedPhysicalTableName, SqlStringGenerationContext context)

protected String
buildSelectQuery(String formattedPhysicalTableName, SqlStringGenerationContext context)

protected String
buildUpdateQuery(String formattedPhysicalTableName, SqlStringGenerationContext context)

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 String

Returns:

The default segment value to use.
determineDefaultSegmentValue
(Properties
The params supplied in the generator config (plus some standard useful extras).
params
)

Used in the cases where determineSegmentValue is unable to determine the value to use.

protected QualifiedName

Returns:

The table name to use.
determineGeneratorTableName
(Properties
The params supplied in the generator config (plus some standard useful extras).
params
,
JdbcEnvironment
The JDBC environment
jdbcEnvironment
,
ServiceRegistry serviceRegistry)

Determine the table name to use for the generator values.

protected int
protected int
private static OptimizerDescriptor
determineOptimizationStrategy(Properties parameters, int incrementSize)

protected String

Returns:

The name of the segment column
determineSegmentColumnName
(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 indicate the segment for each row.

protected int

Returns:

The size of the segment column
determineSegmentColumnSize
(Properties
The params supplied in the generator config (plus some standard useful extras).
params
)

Determine the size of the segment column

Called during configuration.

protected String

Returns:

The name of the value column
determineSegmentValue
(Properties
The params supplied in the generator config (plus some standard useful extras).
params
)

Determine the segment value corresponding to this generator instance.

protected String

Returns:

The name of the value column
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 in which we will store the generator persistent value.

private ResultSet
private int
public Object
generate(final SharedSessionContractImplementor
The session from which the request originates
session
,
final Object
the entity or collection (idbag) for which the id is being generated
obj
)

Implements org.hibernate.id.IdentifierGenerator.generate.

Generate a new identifier.
protected InitCommand
public String[]
getAllSqlForTests()

Deprecated Exposed for tests only.
public final Type

Returns:

The identifier type mapping.
getIdentifierType
()

Type mapping for the identifier.

public final int

Returns:

The increment amount.
getIncrementSize
()

The amount of increment to use.

public final int

Returns:

The initial value to use.
getInitialValue
()

The initial value to use when we find no previous state in the generator table corresponding to our sequence.

public final Optimizer

Returns:

Out optimizer.
getOptimizer
()

Implements org.hibernate.id.OptimizableGenerator.getOptimizer.

The optimizer being used by this generator.
public final String

Returns:

The segment column name
getSegmentColumnName
()

The name of the column in which we store the segment to which each row belongs.

public final String

Returns:

The segment value for this generator instance.
getSegmentValue
()

The value in segment column corresponding to this generator instance.

public final int

Returns:

the column size.
getSegmentValueLength
()

The size of the segment column in the underlying table.

public final long

Returns:

Value for property 'tableAccessCount'.
getTableAccessCount
()

Getter for property 'tableAccessCount'.

public final String

Returns:

The table name.
getTableName
()

The name of the table in which we store this generator's persistent state.

public final String

Returns:

The name of the value column.
getValueColumnName
()

The name of the column in which we store our persistent generator value.

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.
private IntegralDataTypeHolder
private IntegralDataTypeHolder
private PreparedStatement
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.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

accessCountback to summary
private long accessCount
CONFIG_PREFER_SEGMENT_PER_ENTITYback to summary
public static final String CONFIG_PREFER_SEGMENT_PER_ENTITY

By default, in the absence of a segment_value setting, we use a single row for all generators. This setting can be used to change that to instead default to using a row for each entity name.

contributorback to summary
private String contributor
DEF_SEGMENT_COLUMNback to summary
public static final String DEF_SEGMENT_COLUMN

The default segment_column_name value

DEF_SEGMENT_LENGTHback to summary
public static final int DEF_SEGMENT_LENGTH

The default segment_value_length value

DEF_SEGMENT_VALUEback to summary
public static final String DEF_SEGMENT_VALUE

The default segment_value value, unless CONFIG_PREFER_SEGMENT_PER_ENTITY is specified

DEF_TABLEback to summary
public static final String DEF_TABLE

The default table_name value

DEF_VALUE_COLUMNback to summary
public static final String DEF_VALUE_COLUMN

The default value_column_name value

identifierTypeback to summary
private Type identifierType
incrementSizeback to summary
private int incrementSize
initialValueback to summary
private int initialValue
insertQueryback to summary
private String insertQuery
LOGback to summary
private static final CoreMessageLogger LOG
optimizerback to summary
private Optimizer optimizer
physicalTableNameback to summary
private QualifiedName physicalTableName
qualifiedTableNameback to summary
private QualifiedName qualifiedTableName
SEGMENT_COLUMN_PARAMback to summary
public static final String SEGMENT_COLUMN_PARAM

The name of the column which holds the segment key. The segment defines the different buckets (segments) of values currently tracked in the table. The default value is sequence_name

SEGMENT_LENGTH_PARAMback to summary
public static final String SEGMENT_LENGTH_PARAM

Indicates the length of the column defined by SEGMENT_COLUMN_PARAM. Used in schema export. The default value is 255

SEGMENT_VALUE_PARAMback to summary
public static final String SEGMENT_VALUE_PARAM

The value indicating which segment is used by this generator, as indicated by the actual value stored in the column indicated by segment_column_name. The default value for setting is DEF_SEGMENT_VALUE, although prefer_entity_table_as_segment_value effects the default as well.

segmentColumnNameback to summary
private String segmentColumnName
segmentValueback to summary
private String segmentValue
segmentValueLengthback to summary
private int segmentValueLength
selectQueryback to summary
private String selectQuery
storeLastUsedValueback to summary
private boolean storeLastUsedValue
TABLE_PARAMback to summary
public static final String TABLE_PARAM

Configures the name of the table to use. The default value is hibernate_sequences

updateQueryback to summary
private String updateQuery
VALUE_COLUMN_PARAMback to summary
public static final String VALUE_COLUMN_PARAM

The name of column which holds the sequence value. The default value is next_val

valueColumnNameback to summary
private String valueColumnName

Constructor Detail

TableGeneratorback to summary
public TableGenerator()

Method Detail

buildInsertQueryback to summary
protected String buildInsertQuery(String formattedPhysicalTableName, SqlStringGenerationContext context)
buildSelectQueryback to summary
protected String buildSelectQuery(String formattedPhysicalTableName, SqlStringGenerationContext context)
buildUpdateQueryback to summary
protected String buildUpdateQuery(String formattedPhysicalTableName, SqlStringGenerationContext context)
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
determineDefaultSegmentValueback to summary
protected String determineDefaultSegmentValue(Properties params)

Used in the cases where determineSegmentValue is unable to determine the value to use.

Parameters
params:Properties

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

Returns:String

The default segment value to use.

determineGeneratorTableNameback to summary
protected QualifiedName determineGeneratorTableName(Properties params, JdbcEnvironment jdbcEnvironment, ServiceRegistry serviceRegistry)

Determine the table name to use for the generator values.

Called during configuration.

Parameters
params:Properties

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

jdbcEnvironment:JdbcEnvironment

The JDBC environment

Returns:QualifiedName

The table name to use.

See Also
getTableName()
determineIncrementSizeback to summary
protected int determineIncrementSize(Properties params)
determineInitialValueback to summary
protected int determineInitialValue(Properties params)
determineOptimizationStrategyback to summary
private static OptimizerDescriptor determineOptimizationStrategy(Properties parameters, int incrementSize)
determineSegmentColumnNameback to summary
protected String determineSegmentColumnName(Properties params, JdbcEnvironment jdbcEnvironment)

Determine the name of the column used to indicate the segment for each row. This column acts as the primary key.

Called during configuration.

Parameters
params:Properties

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

jdbcEnvironment:JdbcEnvironment

The JDBC environment

Returns:String

The name of the segment column

See Also
getSegmentColumnName()
determineSegmentColumnSizeback to summary
protected int determineSegmentColumnSize(Properties params)

Determine the size of the segment column

Called during configuration.

Parameters
params:Properties

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

Returns:int

The size of the segment column

See Also
getSegmentValueLength()
determineSegmentValueback to summary
protected String determineSegmentValue(Properties params)

Determine the segment value corresponding to this generator instance.

Called during configuration.

Parameters
params:Properties

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

Returns:String

The name of the value column

See Also
getSegmentValue()
determineValueColumnNameback to summary
protected String determineValueColumnName(Properties params, JdbcEnvironment jdbcEnvironment)

Determine the name of the column in which we will store the generator persistent value.

Called during configuration.

Parameters
params:Properties

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

jdbcEnvironment:JdbcEnvironment

The JDBC environment

Returns:String

The name of the value column

See Also
getValueColumnName()
executeQueryback to summary
private ResultSet executeQuery(PreparedStatement ps, SessionEventListenerManager listener, String sql, SharedSessionContractImplementor session) throws SQLException
executeUpdateback to summary
private int executeUpdate(PreparedStatement ps, SessionEventListenerManager listener, String sql, SharedSessionContractImplementor session) throws SQLException
generateback to summary
public Object generate(final SharedSessionContractImplementor session, final Object obj)

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

obj:Object

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

Returns:Object

a new identifier

Annotations
@Override
generateInsertInitCommandback to summary
protected InitCommand generateInsertInitCommand(SqlStringGenerationContext context)
getAllSqlForTestsback to summary
public String[] getAllSqlForTests()

Deprecated

Exposed for tests only.

Annotations
@Deprecated
getIdentifierTypeback to summary
public final Type getIdentifierType()

Type mapping for the identifier.

Returns:Type

The identifier type mapping.

getIncrementSizeback to summary
public final int getIncrementSize()

The amount of increment to use. The exact implications of this depends on the optimizer being used.

Returns:int

The increment amount.

getInitialValueback to summary
public final int getInitialValue()

The initial value to use when we find no previous state in the generator table corresponding to our sequence.

Returns:int

The initial value to use.

getOptimizerback to summary
public final Optimizer getOptimizer()

Implements org.hibernate.id.OptimizableGenerator.getOptimizer.

The optimizer being used by this generator.

Returns:Optimizer

Out optimizer.

Annotations
@Override
getSegmentColumnNameback to summary
public final String getSegmentColumnName()

The name of the column in which we store the segment to which each row belongs. The value here acts as PK.

Returns:String

The segment column name

getSegmentValueback to summary
public final String getSegmentValue()

The value in segment column corresponding to this generator instance. In other words this value indicates the row in which this generator instance will store values.

Returns:String

The segment value for this generator instance.

getSegmentValueLengthback to summary
public final int getSegmentValueLength()

The size of the segment column in the underlying table.

API Note

This should really have been called segmentColumnLength or even better segmentColumnSize.

Returns:int

the column size.

getTableAccessCountback to summary
public final long getTableAccessCount()

Getter for property 'tableAccessCount'. Only really useful for unit test assertions.

Returns:long

Value for property 'tableAccessCount'.

getTableNameback to summary
public final String getTableName()

The name of the table in which we store this generator's persistent state.

Returns:String

The table name.

getValueColumnNameback to summary
public final String getValueColumnName()

The name of the column in which we store our persistent generator value.

Returns:String

The name of the value column.

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
makeValueback to summary
private IntegralDataTypeHolder makeValue()
nextValueback to summary
private IntegralDataTypeHolder nextValue(Connection connection, SqlStatementLogger logger, SessionEventListenerManager listener, SharedSessionContractImplementor session) throws SQLException
prepareStatementback to summary
private PreparedStatement prepareStatement(Connection connection, String sql, SqlStatementLogger logger, SessionEventListenerManager listener, SharedSessionContractImplementor session) throws SQLException
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