Class TableGenerator
- All Implemented Interfaces:
Serializable,ExportableProducer,BeforeExecutionGenerator,Generator,Configurable,IdentifierGenerator,OptimizableGenerator,PersistentIdentifierGenerator
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.
| 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 |
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringBy default, in the absence of a "segment_value" setting, we use a single row for all generators.static final StringThe default "segment_column_name" valuestatic final intThe default "segment_value_length" valuestatic final StringThe default "segment_value" value, unlessCONFIG_PREFER_SEGMENT_PER_ENTITYis specifiedstatic final StringThe default "table_name" valuestatic final StringThe default "value_column_name" valuestatic final StringThe name of the column which holds the segment key.static final StringIndicates the length of the column defined bySEGMENT_COLUMN_PARAM.static final StringThe value indicating which segment is used by this generator, as indicated by the actual value stored in the column indicated by "segment_column_name".static final StringConfigures the name of the table to use.static final StringThe name of column which holds the sequence value.Fields inherited from interface org.hibernate.id.IdentifierGenerator
CONTRIBUTOR_NAME, ENTITY_NAME, GENERATOR_NAME, JPA_ENTITY_NAMEFields inherited from interface org.hibernate.id.OptimizableGenerator
DEFAULT_INCREMENT_SIZE, DEFAULT_INITIAL_VALUE, IMPLICIT_NAME_BASE, INCREMENT_PARAM, INITIAL_PARAM, OPT_PARAMFields inherited from interface org.hibernate.id.PersistentIdentifierGenerator
CATALOG, IDENTIFIER_NORMALIZER, OPTIONS, PK, SCHEMA, TABLE, TABLES -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidapplyConfiguration(TableGenerator generatorConfig, BiConsumer<String, String> configurationCollector) protected StringbuildInsertQuery(String formattedPhysicalTableName, SqlStringGenerationContext context) protected StringbuildSelectQuery(String formattedPhysicalTableName, SqlStringGenerationContext context) protected StringbuildUpdateQuery(String formattedPhysicalTableName, SqlStringGenerationContext context) voidconfigure(GeneratorCreationContext creationContext, Properties parameters) Configure this instance, given the value of parameters specified by the user as XML<param>elements and@Parameterannotations.protected StringUsed in the cases wheredetermineSegmentValue(java.util.Properties)is unable to determine the value to use.protected QualifiedNamedetermineGeneratorTableName(Properties params, JdbcEnvironment jdbcEnvironment, ServiceRegistry serviceRegistry) Determine the table name to use for the generator values.protected intdetermineIncrementSize(Properties params) protected intdetermineInitialValue(Properties params) protected StringdetermineSegmentColumnName(Properties params, JdbcEnvironment jdbcEnvironment) Determine the name of the column used to indicate the segment for each row.protected intdetermineSegmentColumnSize(Properties params) Determine the size of thesegment columnprotected StringdetermineSegmentValue(Properties params) Determine the segment value corresponding to this generator instance.protected StringdetermineValueColumnName(Properties params, JdbcEnvironment jdbcEnvironment) Determine the name of the column in which we will store the generator persistent value.generate(SharedSessionContractImplementor session, Object obj) Generate a new identifier.protected InitCommandString[]Deprecated.Exposed for tests only.final TypeType mapping for the identifier.final intThe amount of increment to use.final intThe initial value to use when we find no previous state in the generator table corresponding to our sequence.final OptimizerThe optimizer being used by this generator.final StringThe name of the column in which we store the segment to which each row belongs.final StringThe value insegment columncorresponding to this generator instance.final intThe size of thesegment columnin the underlying table.final longGetter for property 'tableAccessCount'.final StringThe name of the table in which we store this generator's persistent state.final StringThe name of the column in which we store our persistent generator value.voidinitialize(SqlStringGenerationContext context) Initializes this instance, pre-generating SQL if necessary.voidregisterExportables(Database database) Register database objects used by this identifier generator, for example, a sequence or tables.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.hibernate.generator.BeforeExecutionGenerator
generatedOnExecutionMethods inherited from interface org.hibernate.generator.Generator
allowAssignedIdentifiers, allowMutation, generatedBeforeExecution, generatedOnExecution, generatesOnForceIncrement, generatesOnInsert, generatesOnUpdate, generatesSometimesMethods inherited from interface org.hibernate.id.IdentifierGenerator
configure, generate, getEventTypes
-
Field Details
-
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.- See Also:
-
TABLE_PARAM
Configures the name of the table to use. The default value is "hibernate_sequences"- See Also:
-
DEF_TABLE
The default "table_name" value- See Also:
-
VALUE_COLUMN_PARAM
The name of column which holds the sequence value. The default value is "next_val"- See Also:
-
DEF_VALUE_COLUMN
The default "value_column_name" value- See Also:
-
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"- See Also:
-
DEF_SEGMENT_COLUMN
The default "segment_column_name" value- See Also:
-
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 isDEF_SEGMENT_VALUE, although "prefer_entity_table_as_segment_value" effects the default as well.- See Also:
-
DEF_SEGMENT_VALUE
The default "segment_value" value, unlessCONFIG_PREFER_SEGMENT_PER_ENTITYis specified- See Also:
-
SEGMENT_LENGTH_PARAM
Indicates the length of the column defined bySEGMENT_COLUMN_PARAM. Used in schema export. The default value is 255- See Also:
-
DEF_SEGMENT_LENGTH
public static final int DEF_SEGMENT_LENGTHThe default "segment_value_length" value- See Also:
-
-
Constructor Details
-
TableGenerator
public TableGenerator()
-
-
Method Details
-
getIdentifierType
Type mapping for the identifier.- Returns:
- The identifier type mapping.
-
getTableName
The name of the table in which we store this generator's persistent state.- Returns:
- The table name.
-
getSegmentColumnName
The name of the column in which we store the segment to which each row belongs. The value here acts as PK.- Returns:
- The segment column name
-
getSegmentValue
The value insegment columncorresponding to this generator instance. In other words this value indicates the row in which this generator instance will store values.- Returns:
- The segment value for this generator instance.
-
getSegmentValueLength
public final int getSegmentValueLength()The size of thesegment columnin the underlying table.- Returns:
- the column size.
- API Note:
- This should really have been called
segmentColumnLengthor even bettersegmentColumnSize.
-
getValueColumnName
The name of the column in which we store our persistent generator value.- Returns:
- The name of the value column.
-
getInitialValue
public final int getInitialValue()The initial value to use when we find no previous state in the generator table corresponding to our sequence.- Returns:
- The initial value to use.
-
getIncrementSize
public final int getIncrementSize()The amount of increment to use. The exact implications of this depends on the optimizer being used.- Returns:
- The increment amount.
-
getOptimizer
The optimizer being used by this generator.- Specified by:
getOptimizerin interfaceOptimizableGenerator- Returns:
- Out optimizer.
-
getTableAccessCount
public final long getTableAccessCount()Getter for property 'tableAccessCount'. Only really useful for unit test assertions.- Returns:
- Value for property 'tableAccessCount'.
-
getAllSqlForTests
Deprecated.Exposed for tests only. -
configure
public void configure(GeneratorCreationContext creationContext, Properties parameters) throws MappingException Description copied from interface:ConfigurableConfigure this instance, given the value of parameters specified by the user as XML<param>elements and@Parameterannotations.This method is called just once, following instantiation. If this instance also implements
ExportableProducer, then this method is always called beforeExportableProducer.registerExportables(Database),- Specified by:
configurein interfaceConfigurable- Parameters:
creationContext- Access to the generator creation contextparameters- param values, keyed by parameter name- Throws:
MappingException- when there's something wrong with the given parameters
-
determineGeneratorTableName
protected QualifiedName determineGeneratorTableName(Properties params, JdbcEnvironment jdbcEnvironment, ServiceRegistry serviceRegistry) Determine the table name to use for the generator values.Called during configuration.
- Parameters:
params- The params supplied in the generator config (plus some standard useful extras).jdbcEnvironment- The JDBC environment- Returns:
- The table name to use.
- See Also:
-
determineSegmentColumnName
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- The params supplied in the generator config (plus some standard useful extras).jdbcEnvironment- The JDBC environment- Returns:
- The name of the segment column
- See Also:
-
determineValueColumnName
Determine the name of the column in which we will store the generator persistent value.Called during configuration.
- Parameters:
params- The params supplied in the generator config (plus some standard useful extras).jdbcEnvironment- The JDBC environment- Returns:
- The name of the value column
- See Also:
-
determineSegmentValue
Determine the segment value corresponding to this generator instance.Called during configuration.
- Parameters:
params- The params supplied in the generator config (plus some standard useful extras).- Returns:
- The name of the value column
- See Also:
-
determineDefaultSegmentValue
Used in the cases wheredetermineSegmentValue(java.util.Properties)is unable to determine the value to use.- Parameters:
params- The params supplied in the generator config (plus some standard useful extras).- Returns:
- The default segment value to use.
-
determineSegmentColumnSize
Determine the size of thesegment columnCalled during configuration.
- Parameters:
params- The params supplied in the generator config (plus some standard useful extras).- Returns:
- The size of the segment column
- See Also:
-
determineInitialValue
-
determineIncrementSize
-
buildSelectQuery
protected String buildSelectQuery(String formattedPhysicalTableName, SqlStringGenerationContext context) -
buildUpdateQuery
protected String buildUpdateQuery(String formattedPhysicalTableName, SqlStringGenerationContext context) -
buildInsertQuery
protected String buildInsertQuery(String formattedPhysicalTableName, SqlStringGenerationContext context) -
generateInsertInitCommand
-
registerExportables
Description copied from interface:IdentifierGeneratorRegister database objects used by this identifier generator, for example, a sequence or tables.This method is called just once, after
Configurable.configure(GeneratorCreationContext, Properties).- Specified by:
registerExportablesin interfaceExportableProducer- Specified by:
registerExportablesin interfaceIdentifierGenerator- Parameters:
database- The database instance
-
initialize
Description copied from interface:ConfigurableInitializes this instance, pre-generating SQL if necessary.If this instance also implements
ExportableProducer, then this method is always called afterExportableProducer.registerExportables(Database), and before first use.- Specified by:
initializein interfaceConfigurable- Parameters:
context- A context to help generate SQL strings
-
applyConfiguration
public static void applyConfiguration(TableGenerator generatorConfig, BiConsumer<String, String> configurationCollector)
-