org.hibernate.id.enhanced

Class SequenceStyleGenerator

Implemented Interfaces:
Configurable, IdentifierGenerator, PersistentIdentifierGenerator

public class SequenceStyleGenerator
extends java.lang.Object
implements PersistentIdentifierGenerator, Configurable

Generates identifier values based on an sequence-style database structure. Variations range from actually using a sequence to using a table to mimic a sequence. These variations are encapsulated by the DatabaseStructure interface internally.

General configuration parameters:

NAMEDEFAULTDESCRIPTION
SEQUENCE_PARAMDEF_SEQUENCE_NAMEThe name of the sequence/table to use to store/retrieve values
INITIAL_PARAMDEFAULT_INITIAL_VALUEThe initial value to be stored for the given segment; the effect in terms of storage varies based on Optimizer and DatabaseStructure
INCREMENT_PARAMDEFAULT_INCREMENT_SIZEThe increment size for the underlying segment; the effect in terms of storage varies based on Optimizer and DatabaseStructure
OPT_PARAMdepends on defined increment sizeAllows explicit definition of which optimization strategy to use
FORCE_TBL_PARAMfalseAllows explicit definition of which optimization strategy to use

Configuration parameters used specifically when the underlying structure is a table:

NAMEDEFAULTDESCRIPTION
VALUE_COLUMN_PARAMDEF_VALUE_COLUMNThe name of column which holds the sequence value for the given segment
Author:
Steve Ebersole

Field Summary

static int
DEFAULT_INCREMENT_SIZE
static int
DEFAULT_INITIAL_VALUE
static String
DEF_SEQUENCE_NAME
static String
DEF_VALUE_COLUMN
static String
FORCE_TBL_PARAM
static String
INCREMENT_PARAM
static String
INITIAL_PARAM
static String
OPT_PARAM
static String
SEQUENCE_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 DatabaseStructure
buildDatabaseStructure(Properties params, Dialect dialect, boolean forceTableUse, String sequenceName, int initialValue, int incrementSize)
Build the database structure.
void
configure(Type type, Properties params, Dialect dialect)
protected int
determineAdjustedIncrementSize(String optimizationStrategy, int incrementSize)
In certain cases we need to adjust the increment size based on the selected optimizer.
protected int
determineIncrementSize(Properties params)
Determine the increment size to be applied.
protected int
determineInitialValue(Properties params)
Determine the initial sequence value to use.
protected String
determineOptimizationStrategy(Properties params, int incrementSize)
Determine the optimizer to use.
protected String
determineSequenceName(Properties params)
Determine the name of the sequence (or table if this resolves to a physical table) to use.
protected String
determineValueColumnName(Properties params)
Determine the name of the column used to store the generator value in the db.
Serializable
generate(SessionImplementor session, Object object)
Object
generatorKey()
DatabaseStructure
getDatabaseStructure()
Getter for property 'databaseStructure'.
Type
getIdentifierType()
Getter for property 'identifierType'.
Optimizer
getOptimizer()
Getter for property 'optimizer'.
String[]
sqlCreateStrings(Dialect dialect)
String[]
sqlDropStrings(Dialect dialect)

Field Details

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_SEQUENCE_NAME

public static final String DEF_SEQUENCE_NAME

DEF_VALUE_COLUMN

public static final String DEF_VALUE_COLUMN

FORCE_TBL_PARAM

public static final String FORCE_TBL_PARAM

INCREMENT_PARAM

public static final String INCREMENT_PARAM

INITIAL_PARAM

public static final String INITIAL_PARAM

OPT_PARAM

public static final String OPT_PARAM

SEQUENCE_PARAM

public static final String SEQUENCE_PARAM

VALUE_COLUMN_PARAM

public static final String VALUE_COLUMN_PARAM

Method Details

buildDatabaseStructure

protected DatabaseStructure buildDatabaseStructure(Properties params,
                                                   Dialect dialect,
                                                   boolean forceTableUse,
                                                   String sequenceName,
                                                   int initialValue,
                                                   int incrementSize)
Build the database structure.
Parameters:
params - The params supplied in the generator config (plus some standard useful extras).
dialect - The dialect being used.
forceTableUse - Should a table be used even if the dialect supports sequences?
sequenceName - The name to use for the sequence or table.
initialValue - The initial value.
incrementSize - the increment size to use (after any adjustments).
Returns:
The db structure representation

configure

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

determineAdjustedIncrementSize

protected int determineAdjustedIncrementSize(String optimizationStrategy,
                                             int incrementSize)
In certain cases we need to adjust the increment size based on the selected optimizer. This is the hook to achieve that.
Returns:
The adjusted increment size.

determineIncrementSize

protected int determineIncrementSize(Properties params)
Parameters:
params - The params supplied in the generator config (plus some standard useful extras).
Returns:
The increment size

determineInitialValue

protected int determineInitialValue(Properties params)
Parameters:
params - The params supplied in the generator config (plus some standard useful extras).
Returns:
The initial value

determineOptimizationStrategy

protected String determineOptimizationStrategy(Properties params,
                                               int incrementSize)
Parameters:
params - The params supplied in the generator config (plus some standard useful extras).
incrementSize - The determined increment size
Returns:
The optimizer strategy (name)

determineSequenceName

protected String determineSequenceName(Properties params)
Parameters:
params - The params supplied in the generator config (plus some standard useful extras).
Returns:
The sequence name

determineValueColumnName

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

generate

public Serializable generate(SessionImplementor session,
                             Object object)
            throws HibernateException
Specified by:
generate in interface IdentifierGenerator

generatorKey

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

getDatabaseStructure

public DatabaseStructure getDatabaseStructure()
Getter for property 'databaseStructure'.
Returns:
Value for property 'databaseStructure'.

getIdentifierType

public Type getIdentifierType()
Getter for property 'identifierType'.
Returns:
Value for property 'identifierType'.

getOptimizer

public Optimizer getOptimizer()
Getter for property 'optimizer'.
Returns:
Value for property 'optimizer'.

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