Package org.hibernate.id.enhanced
Class TableGenerator
- java.lang.Object
-
- org.hibernate.id.enhanced.TableGenerator
-
- All Implemented Interfaces:
ExportableProducer
,Configurable
,IdentifierGenerator
,PersistentIdentifierGenerator
public class TableGenerator extends java.lang.Object implements PersistentIdentifierGenerator, Configurable
An enhanced version of table-based id generation. Unlike the simplistic legacy one (which, btw, 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. In this respect it is very similar to the legacyMultipleHiLoPerTableGenerator
in terms of the underlying storage structure (namely a single table capable of holding multiple generator values). The differentiator is, as withSequenceStyleGenerator
as well, the externalized notion of an optimizer. NOTE that by default we use a single row for all generators (based onDEF_SEGMENT_VALUE
). The configuration parameterCONFIG_PREFER_SEGMENT_PER_ENTITY
can be used to change that to instead default to using a row for each entity name. Configuration parameters:NAME DEFAULT DESCRIPTION TABLE_PARAM
DEF_TABLE
The name of the table to use to store/retrieve values VALUE_COLUMN_PARAM
DEF_VALUE_COLUMN
The name of column which holds the sequence value for the given segment SEGMENT_COLUMN_PARAM
DEF_SEGMENT_COLUMN
The name of the column which holds the segment key SEGMENT_VALUE_PARAM
DEF_SEGMENT_VALUE
The value indicating which segment is used by this generator; refers to values in the SEGMENT_COLUMN_PARAM
columnSEGMENT_LENGTH_PARAM
DEF_SEGMENT_LENGTH
The data length of the SEGMENT_COLUMN_PARAM
column; used for schema creationINITIAL_PARAM
DEFAULT_INITIAL_VALUE
The initial value to be stored for the given segment INCREMENT_PARAM
DEFAULT_INCREMENT_SIZE
The increment size for the underlying segment; see the discussion on Optimizer
for more details.OPT_PARAM
depends on defined increment size Allows explicit definition of which optimization strategy to use
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
CONFIG_PREFER_SEGMENT_PER_ENTITY
By default (in the absence of aSEGMENT_VALUE_PARAM
setting) we use a single row for all generators.static java.lang.String
DEF_SEGMENT_COLUMN
The defaultSEGMENT_COLUMN_PARAM
valuestatic int
DEF_SEGMENT_LENGTH
The defaultSEGMENT_LENGTH_PARAM
valuestatic java.lang.String
DEF_SEGMENT_VALUE
The defaultSEGMENT_VALUE_PARAM
value, unlessCONFIG_PREFER_SEGMENT_PER_ENTITY
is specifiedstatic java.lang.String
DEF_TABLE
The defaultTABLE_PARAM
valuestatic java.lang.String
DEF_VALUE_COLUMN
The defaultVALUE_COLUMN_PARAM
valuestatic int
DEFAULT_INCREMENT_SIZE
The defaultINCREMENT_PARAM
valuestatic int
DEFAULT_INITIAL_VALUE
The defaultINITIAL_PARAM
valuestatic java.lang.String
INCREMENT_PARAM
Indicates the increment size to use.static java.lang.String
INITIAL_PARAM
Indicates the initial value to use.static java.lang.String
OPT_PARAM
Indicates the optimizer to use, either naming aOptimizer
implementation class or by naming aStandardOptimizerDescriptor
by namestatic java.lang.String
SEGMENT_COLUMN_PARAM
The name of the column which holds the segment key.static java.lang.String
SEGMENT_LENGTH_PARAM
Indicates the length of the column defined bySEGMENT_COLUMN_PARAM
.static java.lang.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 bySEGMENT_COLUMN_PARAM
.static java.lang.String
TABLE_PARAM
Configures the name of the table to use.static java.lang.String
VALUE_COLUMN_PARAM
The name of column which holds the sequence value.-
Fields inherited from interface org.hibernate.id.IdentifierGenerator
ENTITY_NAME, GENERATOR_NAME, JPA_ENTITY_NAME
-
Fields inherited from interface org.hibernate.id.PersistentIdentifierGenerator
CATALOG, IDENTIFIER_NORMALIZER, PK, SCHEMA, TABLE, TABLES
-
-
Constructor Summary
Constructors Constructor Description TableGenerator()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected java.lang.String
buildInsertQuery()
protected java.lang.String
buildSelectQuery(Dialect dialect)
protected java.lang.String
buildUpdateQuery()
void
configure(Type type, java.util.Properties params, ServiceRegistry serviceRegistry)
Configure this instance, given the value of parameters specified by the user as <param> elements.protected java.lang.String
determineDefaultSegmentValue(java.util.Properties params)
Used in the cases wheredetermineSegmentValue(java.util.Properties)
is unable to determine the value to use.protected QualifiedName
determineGeneratorTableName(java.util.Properties params, JdbcEnvironment jdbcEnvironment, ServiceRegistry serviceRegistry)
Determine the table name to use for the generator values.protected int
determineIncrementSize(java.util.Properties params)
protected int
determineInitialValue(java.util.Properties params)
protected java.lang.String
determineSegmentColumnName(java.util.Properties params, JdbcEnvironment jdbcEnvironment)
Determine the name of the column used to indicate the segment for each row.protected int
determineSegmentColumnSize(java.util.Properties params)
Determine the size of thesegment column
protected java.lang.String
determineSegmentValue(java.util.Properties params)
Determine the segment value corresponding to this generator instance.protected java.lang.String
determineValueColumnName(java.util.Properties params, JdbcEnvironment jdbcEnvironment)
Determine the name of the column in which we will store the generator persistent value.java.io.Serializable
generate(SharedSessionContractImplementor session, java.lang.Object obj)
Generate a new identifier.protected InitCommand
generateInsertInitCommand()
java.lang.Object
generatorKey()
Return a key unique to the underlying database objects.Type
getIdentifierType()
Type mapping for the identifier.int
getIncrementSize()
The amount of increment to use.int
getInitialValue()
The initial value to use when we find no previous state in the generator table corresponding to our sequence.Optimizer
getOptimizer()
The optimizer being used by this generator.java.lang.String
getSegmentColumnName()
The name of the column in which we store the segment to which each row belongs.java.lang.String
getSegmentValue()
The value insegment column
which corresponding to this generator instance.int
getSegmentValueLength()
The size of thesegment column
in the underlying table.long
getTableAccessCount()
Getter for property 'tableAccessCount'.java.lang.String
getTableName()
The name of the table in which we store this generator's persistent state.java.lang.String
getValueColumnName()
The name of the column in which we store our persistent generator value.void
registerExportables(Database database)
Register the contained exportable things to theDatabase
java.lang.String[]
sqlCreateStrings(Dialect dialect)
The SQL required to create the underlying database objects.java.lang.String[]
sqlDropStrings(Dialect dialect)
The SQL required to remove the underlying database objects.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.hibernate.id.IdentifierGenerator
supportsJdbcBatchInserts
-
-
-
-
Field Detail
-
CONFIG_PREFER_SEGMENT_PER_ENTITY
public static final java.lang.String CONFIG_PREFER_SEGMENT_PER_ENTITY
By default (in the absence of aSEGMENT_VALUE_PARAM
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:
- Constant Field Values
-
TABLE_PARAM
public static final java.lang.String TABLE_PARAM
Configures the name of the table to use. The default value isDEF_TABLE
- See Also:
- Constant Field Values
-
DEF_TABLE
public static final java.lang.String DEF_TABLE
The defaultTABLE_PARAM
value- See Also:
- Constant Field Values
-
VALUE_COLUMN_PARAM
public static final java.lang.String VALUE_COLUMN_PARAM
The name of column which holds the sequence value. The default value isDEF_VALUE_COLUMN
- See Also:
- Constant Field Values
-
DEF_VALUE_COLUMN
public static final java.lang.String DEF_VALUE_COLUMN
The defaultVALUE_COLUMN_PARAM
value- See Also:
- Constant Field Values
-
SEGMENT_COLUMN_PARAM
public static final java.lang.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 isDEF_SEGMENT_COLUMN
- See Also:
- Constant Field Values
-
DEF_SEGMENT_COLUMN
public static final java.lang.String DEF_SEGMENT_COLUMN
The defaultSEGMENT_COLUMN_PARAM
value- See Also:
- Constant Field Values
-
SEGMENT_VALUE_PARAM
public static final java.lang.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 bySEGMENT_COLUMN_PARAM
. The default value for setting isDEF_SEGMENT_VALUE
, althoughCONFIG_PREFER_SEGMENT_PER_ENTITY
effects the default as well.- See Also:
- Constant Field Values
-
DEF_SEGMENT_VALUE
public static final java.lang.String DEF_SEGMENT_VALUE
The defaultSEGMENT_VALUE_PARAM
value, unlessCONFIG_PREFER_SEGMENT_PER_ENTITY
is specified- See Also:
- Constant Field Values
-
SEGMENT_LENGTH_PARAM
public static final java.lang.String SEGMENT_LENGTH_PARAM
Indicates the length of the column defined bySEGMENT_COLUMN_PARAM
. Used in schema export. The default value isDEF_SEGMENT_LENGTH
- See Also:
- Constant Field Values
-
DEF_SEGMENT_LENGTH
public static final int DEF_SEGMENT_LENGTH
The defaultSEGMENT_LENGTH_PARAM
value- See Also:
- Constant Field Values
-
INITIAL_PARAM
public static final java.lang.String INITIAL_PARAM
Indicates the initial value to use. The default value isDEFAULT_INITIAL_VALUE
- See Also:
- Constant Field Values
-
DEFAULT_INITIAL_VALUE
public static final int DEFAULT_INITIAL_VALUE
The defaultINITIAL_PARAM
value- See Also:
- Constant Field Values
-
INCREMENT_PARAM
public static final java.lang.String INCREMENT_PARAM
Indicates the increment size to use. The default value isDEFAULT_INCREMENT_SIZE
- See Also:
- Constant Field Values
-
DEFAULT_INCREMENT_SIZE
public static final int DEFAULT_INCREMENT_SIZE
The defaultINCREMENT_PARAM
value- See Also:
- Constant Field Values
-
OPT_PARAM
public static final java.lang.String OPT_PARAM
Indicates the optimizer to use, either naming aOptimizer
implementation class or by naming aStandardOptimizerDescriptor
by name- See Also:
- Constant Field Values
-
-
Method Detail
-
generatorKey
public java.lang.Object generatorKey()
Description copied from interface:PersistentIdentifierGenerator
Return a key unique to the underlying database objects. Prevents us from trying to create/remove them multiple times.- Specified by:
generatorKey
in interfacePersistentIdentifierGenerator
- Returns:
- Object an identifying key for this generator
-
getIdentifierType
public final Type getIdentifierType()
Type mapping for the identifier.- Returns:
- The identifier type mapping.
-
getTableName
public final java.lang.String getTableName()
The name of the table in which we store this generator's persistent state.- Returns:
- The table name.
-
getSegmentColumnName
public final java.lang.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:
- The segment column name
-
getSegmentValue
public final java.lang.String getSegmentValue()
The value insegment column
which corresponding 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 column
in the underlying table. NOTE : should really have been called 'segmentColumnLength' or even better 'segmentColumnSize'- Returns:
- the column size.
-
getValueColumnName
public final java.lang.String 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 theoptimizer
being used.- Returns:
- The increment amount.
-
getOptimizer
public final Optimizer getOptimizer()
The optimizer being used by this generator.- Returns:
- Out optimizer.
-
getTableAccessCount
public final long getTableAccessCount()
Getter for property 'tableAccessCount'. Only really useful for unit test assertions.- Returns:
- Value for property 'tableAccessCount'.
-
configure
public void configure(Type type, java.util.Properties params, ServiceRegistry serviceRegistry) throws MappingException
Description copied from interface:Configurable
Configure this instance, given the value of parameters specified by the user as <param> elements. This method is called just once, following instantiation.- Specified by:
configure
in interfaceConfigurable
- Parameters:
type
- The id property type descriptorparams
- param values, keyed by parameter nameserviceRegistry
- Access to service that may be needed.- Throws:
MappingException
-
determineGeneratorTableName
protected QualifiedName determineGeneratorTableName(java.util.Properties params, JdbcEnvironment jdbcEnvironment, ServiceRegistry serviceRegistry)
Determine the table name to use for the generator values. Called duringconfiguration
.- 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:
getTableName()
-
determineSegmentColumnName
protected java.lang.String determineSegmentColumnName(java.util.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 duringconfiguration
.- 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:
getSegmentColumnName()
-
determineValueColumnName
protected java.lang.String determineValueColumnName(java.util.Properties params, JdbcEnvironment jdbcEnvironment)
Determine the name of the column in which we will store the generator persistent value. Called duringconfiguration
.- 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:
getValueColumnName()
-
determineSegmentValue
protected java.lang.String determineSegmentValue(java.util.Properties params)
Determine the segment value corresponding to this generator instance. Called duringconfiguration
.- Parameters:
params
- The params supplied in the generator config (plus some standard useful extras).- Returns:
- The name of the value column
- See Also:
getSegmentValue()
-
determineDefaultSegmentValue
protected java.lang.String determineDefaultSegmentValue(java.util.Properties params)
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
protected int determineSegmentColumnSize(java.util.Properties params)
Determine the size of thesegment column
Called duringconfiguration
.- Parameters:
params
- The params supplied in the generator config (plus some standard useful extras).- Returns:
- The size of the segment column
- See Also:
getSegmentValueLength()
-
determineInitialValue
protected int determineInitialValue(java.util.Properties params)
-
determineIncrementSize
protected int determineIncrementSize(java.util.Properties params)
-
buildSelectQuery
protected java.lang.String buildSelectQuery(Dialect dialect)
-
buildUpdateQuery
protected java.lang.String buildUpdateQuery()
-
buildInsertQuery
protected java.lang.String buildInsertQuery()
-
generateInsertInitCommand
protected InitCommand generateInsertInitCommand()
-
generate
public java.io.Serializable generate(SharedSessionContractImplementor session, java.lang.Object obj)
Description copied from interface:IdentifierGenerator
Generate a new identifier.- Specified by:
generate
in interfaceIdentifierGenerator
- Parameters:
session
- The session from which the request originatesobj
- the entity or collection (idbag) for which the id is being generated- Returns:
- a new identifier
-
sqlCreateStrings
public java.lang.String[] sqlCreateStrings(Dialect dialect) throws HibernateException
Description copied from interface:PersistentIdentifierGenerator
The SQL required to create the underlying database objects.- Specified by:
sqlCreateStrings
in interfacePersistentIdentifierGenerator
- Parameters:
dialect
- The dialect against which to generate the create command(s)- Returns:
- The create command(s)
- Throws:
HibernateException
- problem creating the create command(s)
-
sqlDropStrings
public java.lang.String[] sqlDropStrings(Dialect dialect) throws HibernateException
Description copied from interface:PersistentIdentifierGenerator
The SQL required to remove the underlying database objects.- Specified by:
sqlDropStrings
in interfacePersistentIdentifierGenerator
- Parameters:
dialect
- The dialect against which to generate the drop command(s)- Returns:
- The drop command(s)
- Throws:
HibernateException
- problem creating the drop command(s)
-
registerExportables
public void registerExportables(Database database)
Description copied from interface:ExportableProducer
Register the contained exportable things to theDatabase
- Specified by:
registerExportables
in interfaceExportableProducer
- Parameters:
database
- The database instance
-
-