org.hibernate.id.enhanced

Class TableGenerator

Implemented Interfaces:
Configurable, IdentifierGenerator, PersistentIdentifierGenerator

public class TableGenerator
extends TransactionHelper
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 simliar 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 genertators (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:

NAMEDEFAULTDESCRIPTION
TABLE_PARAMDEF_TABLEThe name of the table to use to store/retrieve values
VALUE_COLUMN_PARAMDEF_VALUE_COLUMNThe name of column which holds the sequence value for the given segment
SEGMENT_COLUMN_PARAMDEF_SEGMENT_COLUMNThe name of the column which holds the segment key
SEGMENT_VALUE_PARAMDEF_SEGMENT_VALUEThe value indicating which segment is used by this generator; refers to values in the SEGMENT_COLUMN_PARAM column
SEGMENT_LENGTH_PARAMDEF_SEGMENT_LENGTHThe data length of the SEGMENT_COLUMN_PARAM column; used for schema creation
INITIAL_PARAMDEFAULT_INITIAL_VALUEThe initial value to be stored for the given segment
INCREMENT_PARAMDEFAULT_INCREMENT_SIZEThe increment size for the underlying segment; see the discussion on Optimizer for more details.
OPT_PARAMdepends on defined increment sizeAllows explicit definition of which optimization strategy to use
Author:
Steve Ebersole

Field Summary

static String
CONFIG_PREFER_SEGMENT_PER_ENTITY
static int
DEFAULT_INCREMENT_SIZE
static int
DEFAULT_INITIAL_VALUE
static String
DEF_SEGMENT_COLUMN
static int
DEF_SEGMENT_LENGTH
static String
DEF_SEGMENT_VALUE
static String
DEF_TABLE
static String
DEF_VALUE_COLUMN
static String
INCREMENT_PARAM
static String
INITIAL_PARAM
static String
OPT_PARAM
static String
SEGMENT_COLUMN_PARAM
static String
SEGMENT_LENGTH_PARAM
static String
SEGMENT_VALUE_PARAM
static String
TABLE_PARAM
static String
VALUE_COLUMN_PARAM

Fields inherited from interface org.hibernate.id.IdentifierGenerator

ENTITY_NAME

Fields inherited from interface org.hibernate.id.PersistentIdentifierGenerator

CATALOG, PK, SCHEMA, SQL_STATEMENT_LOGGER, TABLE, TABLES

Method Summary

protected String
buildInsertQuery()
protected String
buildSelectQuery(Dialect dialect)
protected String
buildUpdateQuery()
void
configure(Type type, Properties params, Dialect dialect)
protected String
determineDefaultSegmentValue(Properties params)
Used in the cases where determineSegmentValue(Properties) is unable to determine the value to use.
protected int
determineIncrementSize(Properties params)
protected int
determineInitialValue(Properties params)
protected String
determineSegmentColumnName(Properties params)
Determine the name of the column used to indicate the segment for each row.
protected int
determineSegmentColumnSize(Properties params)
Determine the size of the segment column

Called during configuration.

protected String
determineSegmentValue(Properties params)
Determine the segment value corresponding to this generator instance.
protected String
determineValueColumnName(Properties params)
Determine the name of the column in which we will store the generator persistent value.
protected String
determneGeneratorTableName(Properties params)
Determine the table name to use for the generator values.
Serializable
doWorkInCurrentTransaction(Connection conn, String sql)
Serializable
generate(SessionImplementor session, Object obj)
Object
generatorKey()
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.
String
getSegmentColumnName()
The name of the column in which we store the segment to which each row belongs.
String
getSegmentValue()
The value in segment column which corresponding to this generator instance.
int
getSegmentValueLength()
The size of the segment column in the underlying table.
long
getTableAccessCount()
Getter for property 'tableAccessCount'.
String
getTableName()
The name of the table in which we store this generator's persistent state.
String
getValueColumnName()
The name of the column in which we store our persistent generator value.
String[]
sqlCreateStrings(Dialect dialect)
String[]
sqlDropStrings(Dialect dialect)

Methods inherited from class org.hibernate.engine.TransactionHelper

doWorkInCurrentTransaction, doWorkInNewTransaction

Field Details

CONFIG_PREFER_SEGMENT_PER_ENTITY

public static final String CONFIG_PREFER_SEGMENT_PER_ENTITY

DEFAULT_INCREMENT_SIZE

public static final int DEFAULT_INCREMENT_SIZE
Field Value:
1

DEFAULT_INITIAL_VALUE

public static final int DEFAULT_INITIAL_VALUE
Field Value:
1

DEF_SEGMENT_COLUMN

public static final String DEF_SEGMENT_COLUMN

DEF_SEGMENT_LENGTH

public static final int DEF_SEGMENT_LENGTH
Field Value:
255

DEF_SEGMENT_VALUE

public static final String DEF_SEGMENT_VALUE

DEF_TABLE

public static final String DEF_TABLE

DEF_VALUE_COLUMN

public static final String DEF_VALUE_COLUMN

INCREMENT_PARAM

public static final String INCREMENT_PARAM

INITIAL_PARAM

public static final String INITIAL_PARAM

OPT_PARAM

public static final String OPT_PARAM

SEGMENT_COLUMN_PARAM

public static final String SEGMENT_COLUMN_PARAM

SEGMENT_LENGTH_PARAM

public static final String SEGMENT_LENGTH_PARAM

SEGMENT_VALUE_PARAM

public static final String SEGMENT_VALUE_PARAM

TABLE_PARAM

public static final String TABLE_PARAM

VALUE_COLUMN_PARAM

public static final String VALUE_COLUMN_PARAM

Method Details

buildInsertQuery

protected String buildInsertQuery()

buildSelectQuery

protected String buildSelectQuery(Dialect dialect)

buildUpdateQuery

protected String buildUpdateQuery()

configure

public void configure(Type type,
                      Properties params,
                      Dialect dialect)
            throws MappingException
Specified by:
configure in interface Configurable

determineDefaultSegmentValue

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.

determineIncrementSize

protected int determineIncrementSize(Properties params)

determineInitialValue

protected int determineInitialValue(Properties params)

determineSegmentColumnName

protected String determineSegmentColumnName(Properties params)
Parameters:
params - The params supplied in the generator config (plus some standard useful extras).
Returns:
The name of the segment column

determineSegmentColumnSize

protected int determineSegmentColumnSize(Properties params)
Parameters:
params - The params supplied in the generator config (plus some standard useful extras).
Returns:
The size of the segment column

determineSegmentValue

protected String determineSegmentValue(Properties params)
Parameters:
params - The params supplied in the generator config (plus some standard useful extras).
Returns:
The name of the value column

determineValueColumnName

protected String determineValueColumnName(Properties params)
Parameters:
params - The params supplied in the generator config (plus some standard useful extras).
Returns:
The name of the value column

determneGeneratorTableName

protected String determneGeneratorTableName(Properties params)
Parameters:
params - The params supplied in the generator config (plus some standard useful extras).
Returns:
The table name to use.

doWorkInCurrentTransaction

public Serializable doWorkInCurrentTransaction(Connection conn,
                                               String sql)
            throws SQLException
Overrides:
doWorkInCurrentTransaction in interface TransactionHelper

generate

public Serializable generate(SessionImplementor session,
                             Object obj)
Specified by:
generate in interface IdentifierGenerator

generatorKey

public Object generatorKey()
Specified by:
generatorKey in interface PersistentIdentifierGenerator

getIdentifierType

public final Type getIdentifierType()
Type mapping for the identifier.
Returns:
The identifier type mapping.

getIncrementSize

public final int getIncrementSize()
Returns:
The increment amount.

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.

getOptimizer

public final Optimizer getOptimizer()
The optimizer being used by this generator.
Returns:
Out optimizer.

getSegmentColumnName

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

getSegmentValue

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.

getSegmentValueLength

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.

getTableAccessCount

public final long getTableAccessCount()
Getter for property 'tableAccessCount'. Only really useful for unit test assertions.
Returns:
Value for property 'tableAccessCount'.

getTableName

public final String getTableName()
The name of the table in which we store this generator's persistent state.
Returns:
The table name.

getValueColumnName

public final String getValueColumnName()
The name of the column in which we store our persistent generator value.
Returns:
The name of the value column.

sqlCreateStrings

public String[] sqlCreateStrings(Dialect dialect)
            throws HibernateException
Specified by:
sqlCreateStrings in interface PersistentIdentifierGenerator

sqlDropStrings

public String[] sqlDropStrings(Dialect dialect)
            throws HibernateException
Specified by:
sqlDropStrings in interface PersistentIdentifierGenerator