public class

TableGenerator

extends TransactionHelper
implements Configurable PersistentIdentifierGenerator
java.lang.Object
   ↳ org.hibernate.engine.TransactionHelper
     ↳ org.hibernate.id.enhanced.TableGenerator

Class Overview

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 legacy MultipleHiLoPerTableGenerator in terms of the underlying storage structure (namely a single table capable of holding multiple generator values). The differentiator is, as with SequenceStyleGenerator as well, the externalized notion of an optimizer.

NOTE that by default we use a single row for all generators (based on DEF_SEGMENT_VALUE). The configuration parameter CONFIG_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 column
SEGMENT_LENGTH_PARAM DEF_SEGMENT_LENGTH The data length of the SEGMENT_COLUMN_PARAM column; used for schema creation
INITIAL_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

Summary

Constants
String CONFIG_PREFER_SEGMENT_PER_ENTITY
int DEFAULT_INCREMENT_SIZE
int DEFAULT_INITIAL_VALUE
String DEF_SEGMENT_COLUMN
int DEF_SEGMENT_LENGTH
String DEF_SEGMENT_VALUE
String DEF_TABLE
String DEF_VALUE_COLUMN
String INCREMENT_PARAM
String INITIAL_PARAM
String OPT_PARAM
String SEGMENT_COLUMN_PARAM
String SEGMENT_LENGTH_PARAM
String SEGMENT_VALUE_PARAM
String TABLE_PARAM
String VALUE_COLUMN_PARAM
[Expand]
Inherited Constants
From interface org.hibernate.id.IdentifierGenerator
From interface org.hibernate.id.PersistentIdentifierGenerator
[Expand]
Inherited Fields
From interface org.hibernate.id.PersistentIdentifierGenerator
Public Constructors
TableGenerator()
Public Methods
void configure(Type type, Properties params, Dialect dialect)
Configure this instance, given the value of parameters specified by the user as <param> elements.
Serializable doWorkInCurrentTransaction(Connection conn, String sql)
The work to be done
synchronized Serializable generate(SessionImplementor session, Object obj)
Generate a new identifier.
Object generatorKey()
Return a key unique to the underlying database objects.
final Type getIdentifierType()
Type mapping for the identifier.
final int getIncrementSize()
The amount of increment to use.
final int getInitialValue()
The initial value to use when we find no previous state in the generator table corresponding to our sequence.
final Optimizer getOptimizer()
The optimizer being used by this generator.
final String getSegmentColumnName()
The name of the column in which we store the segment to which each row belongs.
final String getSegmentValue()
The value in segment column which corresponding to this generator instance.
final int getSegmentValueLength()
The size of the segment column in the underlying table.
final long getTableAccessCount()
Getter for property 'tableAccessCount'.
final String getTableName()
The name of the table in which we store this generator's persistent state.
final String getValueColumnName()
The name of the column in which we store our persistent generator value.
String[] sqlCreateStrings(Dialect dialect)
The SQL required to create the underlying database objects.
String[] sqlDropStrings(Dialect dialect)
The SQL required to remove the underlying database objects.
Protected Methods
String buildInsertQuery()
String buildSelectQuery(Dialect dialect)
String buildUpdateQuery()
String determineDefaultSegmentValue(Properties params)
Used in the cases where determineSegmentValue(Properties) is unable to determine the value to use.
String determineGeneratorTableName(Properties params, Dialect dialect)
Determine the table name to use for the generator values.
int determineIncrementSize(Properties params)
int determineInitialValue(Properties params)
String determineSegmentColumnName(Properties params, Dialect dialect)
Determine the name of the column used to indicate the segment for each row.
int determineSegmentColumnSize(Properties params)
Determine the size of the segment column

Called during configuration.

String determineSegmentValue(Properties params)
Determine the segment value corresponding to this generator instance.
String determineValueColumnName(Properties params, Dialect dialect)
Determine the name of the column in which we will store the generator persistent value.
[Expand]
Inherited Methods
From class org.hibernate.engine.TransactionHelper
From class java.lang.Object
From interface org.hibernate.id.Configurable
From interface org.hibernate.id.IdentifierGenerator
From interface org.hibernate.id.PersistentIdentifierGenerator

Constants

public static final String CONFIG_PREFER_SEGMENT_PER_ENTITY

Constant Value: "prefer_entity_table_as_segment_value"

public static final int DEFAULT_INCREMENT_SIZE

Constant Value: 1 (0x00000001)

public static final int DEFAULT_INITIAL_VALUE

Constant Value: 1 (0x00000001)

public static final String DEF_SEGMENT_COLUMN

Constant Value: "sequence_name"

public static final int DEF_SEGMENT_LENGTH

Constant Value: 255 (0x000000ff)

public static final String DEF_SEGMENT_VALUE

Constant Value: "default"

public static final String DEF_TABLE

Constant Value: "hibernate_sequences"

public static final String DEF_VALUE_COLUMN

Constant Value: "next_val"

public static final String INCREMENT_PARAM

Constant Value: "increment_size"

public static final String INITIAL_PARAM

Constant Value: "initial_value"

public static final String OPT_PARAM

Constant Value: "optimizer"

public static final String SEGMENT_COLUMN_PARAM

Constant Value: "segment_column_name"

public static final String SEGMENT_LENGTH_PARAM

Constant Value: "segment_value_length"

public static final String SEGMENT_VALUE_PARAM

Constant Value: "segment_value"

public static final String TABLE_PARAM

Constant Value: "table_name"

public static final String VALUE_COLUMN_PARAM

Constant Value: "value_column_name"

Public Constructors

public TableGenerator ()

Public Methods

public void configure (Type type, Properties params, Dialect dialect)

Configure this instance, given the value of parameters specified by the user as <param> elements. This method is called just once, following instantiation.

Parameters
params param values, keyed by parameter name

public Serializable doWorkInCurrentTransaction (Connection conn, String sql)

The work to be done

Throws
SQLException

public synchronized Serializable generate (SessionImplementor session, Object obj)

Generate a new identifier.

Parameters
obj the entity or toplevel collection for which the id is being generated
Returns
  • a new identifier

public Object generatorKey ()

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

Returns
  • Object an identifying key for this generator

public final Type getIdentifierType ()

Type mapping for the identifier.

Returns
  • The identifier type mapping.

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.

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.

public final Optimizer getOptimizer ()

The optimizer being used by this generator.

Returns
  • Out optimizer.

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
  • The segment column name

public final String getSegmentValue ()

The value in segment 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.

public final int getSegmentValueLength ()

The size of the segment column in the underlying table.

NOTE : should really have been called 'segmentColumnLength' or even better 'segmentColumnSize'

Returns
  • the column size.

public final long getTableAccessCount ()

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

Returns
  • Value for property 'tableAccessCount'.

public final String getTableName ()

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

Returns
  • The table name.

public final String getValueColumnName ()

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

Returns
  • The name of the value column.

public String[] sqlCreateStrings (Dialect dialect)

The SQL required to create the underlying database objects.

Parameters
dialect The dialect against which to generate the create command(s)
Returns
  • The create command(s)

public String[] sqlDropStrings (Dialect dialect)

The SQL required to remove the underlying database objects.

Parameters
dialect The dialect against which to generate the drop command(s)
Returns
  • The drop command(s)

Protected Methods

protected String buildInsertQuery ()

protected String buildSelectQuery (Dialect dialect)

protected String buildUpdateQuery ()

protected String determineDefaultSegmentValue (Properties params)

Used in the cases where determineSegmentValue(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.

protected String determineGeneratorTableName (Properties params, Dialect dialect)

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).
dialect The dialect in effect
Returns
  • The table name to use.
See Also

protected int determineIncrementSize (Properties params)

protected int determineInitialValue (Properties params)

protected String determineSegmentColumnName (Properties params, Dialect dialect)

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).
dialect The dialect in effect
Returns
  • The name of the segment column

protected int determineSegmentColumnSize (Properties params)

Determine the size of the segment column

Called during configuration.

Parameters
params The params supplied in the generator config (plus some standard useful extras).
Returns
  • The size of the segment column

protected String determineSegmentValue (Properties params)

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

protected String determineValueColumnName (Properties params, Dialect dialect)

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).
dialect The dialect in effect
Returns
  • The name of the value column