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

public Class TableGenerator

extends TransactionHelper
implements PersistentIdentifierGenerator, Configurable
Class Inheritance
All Implemented Interfaces
org.hibernate.id.Configurable, org.hibernate.id.PersistentIdentifierGenerator, org.hibernate.id.IdentifierGenerator
Imports
java.sql.Types, .Connection, .SQLException, .PreparedStatement, .ResultSet, java.util.Properties, .Collections, .Map, java.io.Serializable, org.slf4j.Logger, .LoggerFactory, org.hibernate.cfg.Environment, .ObjectNameNormalizer, org.hibernate.engine.TransactionHelper, .SessionImplementor, org.hibernate.id.IdentifierGeneratorHelper, .IntegralDataTypeHolder, .PersistentIdentifierGenerator, .Configurable, org.hibernate.type.Type, org.hibernate.dialect.Dialect, org.hibernate.HibernateException, .MappingException, .LockOptions, .LockMode, org.hibernate.jdbc.util.FormatStyle, org.hibernate.mapping.Table, org.hibernate.util.PropertiesHelper, .StringHelper

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 org.hibernate.id.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
Author
Steve Ebersole

Field Summary

Modifier and TypeField and Description
private long
public static final String
public static final String
public static final int
public static final String
public static final String
public static final String
public static final int
public static final int
private Type
public static final String
private int
public static final String
private int
private String
private static final Logger
public static final String
private Optimizer
public static final String
public static final String
public static final String
private String
private String
private int
private String
public static final String
private String
private String
public static final String
private String

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected String
protected String
protected String
public void
configure(Type type, Properties
param values, keyed by parameter name
params
,
Dialect dialect)

Implements org.hibernate.id.Configurable.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 String

Returns:

The table name to use.
determineGeneratorTableName
(Properties
The params supplied in the generator config (plus some standard useful extras).
params
,
Dialect
The dialect in effect
dialect
)

Determine the table name to use for the generator values.

protected int
protected int
protected String

Returns:

The name of the segment column
determineSegmentColumnName
(Properties
The params supplied in the generator config (plus some standard useful extras).
params
,
Dialect
The dialect in effect
dialect
)

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
,
Dialect
The dialect in effect
dialect
)

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

public Serializable
public synchronized Serializable
generate(final SessionImplementor session, Object
the entity or toplevel collection for which the id is being generated
obj
)

Implements org.hibernate.id.IdentifierGenerator.generate.

Generate a new identifier.
public Object
generatorKey()

Implements org.hibernate.id.PersistentIdentifierGenerator.generatorKey.

Return a key unique to the underlying database objects.
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
()

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 which 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 String[]
sqlCreateStrings(Dialect
The dialect against which to generate the create command(s)
dialect
)

Implements org.hibernate.id.PersistentIdentifierGenerator.sqlCreateStrings.

The SQL required to create the underlying database objects.
public String[]
sqlDropStrings(Dialect
The dialect against which to generate the drop command(s)
dialect
)

Implements org.hibernate.id.PersistentIdentifierGenerator.sqlDropStrings.

The SQL required to remove the underlying database objects.
Inherited from org.hibernate.engine.TransactionHelper:
doWorkInNewTransaction

Field Detail

accessCountback to summary
private long accessCount
CONFIG_PREFER_SEGMENT_PER_ENTITYback to summary
public static final String CONFIG_PREFER_SEGMENT_PER_ENTITY
DEF_SEGMENT_COLUMNback to summary
public static final String DEF_SEGMENT_COLUMN
DEF_SEGMENT_LENGTHback to summary
public static final int DEF_SEGMENT_LENGTH
DEF_SEGMENT_VALUEback to summary
public static final String DEF_SEGMENT_VALUE
DEF_TABLEback to summary
public static final String DEF_TABLE
DEF_VALUE_COLUMNback to summary
public static final String DEF_VALUE_COLUMN
DEFAULT_INCREMENT_SIZEback to summary
public static final int DEFAULT_INCREMENT_SIZE
DEFAULT_INITIAL_VALUEback to summary
public static final int DEFAULT_INITIAL_VALUE
identifierTypeback to summary
private Type identifierType
INCREMENT_PARAMback to summary
public static final String INCREMENT_PARAM
incrementSizeback to summary
private int incrementSize
INITIAL_PARAMback to summary
public static final String INITIAL_PARAM
initialValueback to summary
private int initialValue
insertQueryback to summary
private String insertQuery
logback to summary
private static final Logger log
OPT_PARAMback to summary
public static final String OPT_PARAM
optimizerback to summary
private Optimizer optimizer
SEGMENT_COLUMN_PARAMback to summary
public static final String SEGMENT_COLUMN_PARAM
SEGMENT_LENGTH_PARAMback to summary
public static final String SEGMENT_LENGTH_PARAM
SEGMENT_VALUE_PARAMback to summary
public static final String SEGMENT_VALUE_PARAM
segmentColumnNameback to summary
private String segmentColumnName
segmentValueback to summary
private String segmentValue
segmentValueLengthback to summary
private int segmentValueLength
selectQueryback to summary
private String selectQuery
TABLE_PARAMback to summary
public static final String TABLE_PARAM
tableNameback to summary
private String tableName
updateQueryback to summary
private String updateQuery
VALUE_COLUMN_PARAMback to summary
public static final String VALUE_COLUMN_PARAM
valueColumnNameback to summary
private String valueColumnName

Constructor Detail

TableGeneratorback to summary
public TableGenerator()

Method Detail

buildInsertQueryback to summary
protected String buildInsertQuery()
buildSelectQueryback to summary
protected String buildSelectQuery(Dialect dialect)
buildUpdateQueryback to summary
protected String buildUpdateQuery()
configureback to summary
public void configure(Type type, Properties params, Dialect dialect) throws MappingException

Implements org.hibernate.id.Configurable.configure.

Doc from org.hibernate.id.Configurable.configure.

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:Properties

param values, keyed by parameter name

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 String determineGeneratorTableName(Properties params, Dialect dialect)

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).

dialect:Dialect

The dialect in effect

Returns:String

The table name to use.

See Also
getTableName()
determineIncrementSizeback to summary
protected int determineIncrementSize(Properties params)
determineInitialValueback to summary
protected int determineInitialValue(Properties params)
determineSegmentColumnNameback to summary
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:Properties

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

dialect:Dialect

The dialect in effect

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, Dialect dialect)

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).

dialect:Dialect

The dialect in effect

Returns:String

The name of the value column

See Also
getValueColumnName()
doWorkInCurrentTransactionback to summary
public Serializable doWorkInCurrentTransaction(Connection conn, String sql) throws SQLException

Implements abstract org.hibernate.engine.TransactionHelper.doWorkInCurrentTransaction.

Doc from org.hibernate.engine.TransactionHelper.doWorkInCurrentTransaction.

The work to be done

generateback to summary
public synchronized Serializable generate(final SessionImplementor session, Object obj)

Implements org.hibernate.id.IdentifierGenerator.generate.

Doc from org.hibernate.id.IdentifierGenerator.generate.

Generate a new identifier.

Parameters
obj:Object

the entity or toplevel collection for which the id is being generated

Returns:Serializable

a new identifier

generatorKeyback to summary
public Object generatorKey()

Implements org.hibernate.id.PersistentIdentifierGenerator.generatorKey.

Doc from org.hibernate.id.PersistentIdentifierGenerator.generatorKey.

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

Returns:Object

Object an identifying key for this generator

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()

The optimizer being used by this generator.

Returns:Optimizer

Out optimizer.

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 which 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.

Note

: 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.

sqlCreateStringsback to summary
public String[] sqlCreateStrings(Dialect dialect) throws HibernateException

Implements org.hibernate.id.PersistentIdentifierGenerator.sqlCreateStrings.

Doc from org.hibernate.id.PersistentIdentifierGenerator.sqlCreateStrings.

The SQL required to create the underlying database objects.

Parameters
dialect:Dialect

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

Returns:String[]

The create command(s)

Exceptions
HibernateException:
problem creating the create command(s)
sqlDropStringsback to summary
public String[] sqlDropStrings(Dialect dialect) throws HibernateException

Implements org.hibernate.id.PersistentIdentifierGenerator.sqlDropStrings.

Doc from org.hibernate.id.PersistentIdentifierGenerator.sqlDropStrings.

The SQL required to remove the underlying database objects.

Parameters
dialect:Dialect

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

Returns:String[]

The drop command(s)

Exceptions
HibernateException:
problem creating the drop command(s)